Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * linux/arch/m68k/sun3/sun3dvma.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2000 Sam Creasey |
| 5 | * |
| 6 | * Contains common routines for sun3/sun3x DVMA management. |
| 7 | */ |
| 8 | |
Geert Uytterhoeven | 7b3e8de | 2013-06-25 20:33:44 +0200 | [diff] [blame] | 9 | #include <linux/bootmem.h> |
Geert Uytterhoeven | a4df02a | 2013-06-25 21:15:24 +0200 | [diff] [blame] | 10 | #include <linux/init.h> |
Al Viro | 2e81148 | 2006-10-11 17:28:27 +0100 | [diff] [blame] | 11 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/mm.h> |
| 15 | #include <linux/list.h> |
| 16 | |
| 17 | #include <asm/page.h> |
| 18 | #include <asm/pgtable.h> |
| 19 | #include <asm/dvma.h> |
| 20 | |
| 21 | #undef DVMA_DEBUG |
| 22 | |
| 23 | #ifdef CONFIG_SUN3X |
| 24 | extern void dvma_unmap_iommu(unsigned long baddr, int len); |
| 25 | #else |
| 26 | static inline void dvma_unmap_iommu(unsigned long a, int b) |
| 27 | { |
| 28 | } |
| 29 | #endif |
| 30 | |
| 31 | #ifdef CONFIG_SUN3 |
| 32 | extern void sun3_dvma_init(void); |
| 33 | #endif |
| 34 | |
Geert Uytterhoeven | 7b3e8de | 2013-06-25 20:33:44 +0200 | [diff] [blame] | 35 | static unsigned long *iommu_use; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #define dvma_index(baddr) ((baddr - DVMA_START) >> DVMA_PAGE_SHIFT) |
| 38 | |
| 39 | #define dvma_entry_use(baddr) (iommu_use[dvma_index(baddr)]) |
| 40 | |
| 41 | struct hole { |
| 42 | unsigned long start; |
| 43 | unsigned long end; |
| 44 | unsigned long size; |
| 45 | struct list_head list; |
| 46 | }; |
| 47 | |
| 48 | static struct list_head hole_list; |
| 49 | static struct list_head hole_cache; |
| 50 | static struct hole initholes[64]; |
| 51 | |
| 52 | #ifdef DVMA_DEBUG |
| 53 | |
| 54 | static unsigned long dvma_allocs; |
| 55 | static unsigned long dvma_frees; |
| 56 | static unsigned long long dvma_alloc_bytes; |
| 57 | static unsigned long long dvma_free_bytes; |
| 58 | |
| 59 | static void print_use(void) |
| 60 | { |
| 61 | |
| 62 | int i; |
| 63 | int j = 0; |
| 64 | |
| 65 | printk("dvma entry usage:\n"); |
| 66 | |
| 67 | for(i = 0; i < IOMMU_TOTAL_ENTRIES; i++) { |
| 68 | if(!iommu_use[i]) |
| 69 | continue; |
| 70 | |
| 71 | j++; |
| 72 | |
| 73 | printk("dvma entry: %08lx len %08lx\n", |
| 74 | ( i << DVMA_PAGE_SHIFT) + DVMA_START, |
| 75 | iommu_use[i]); |
| 76 | } |
| 77 | |
| 78 | printk("%d entries in use total\n", j); |
| 79 | |
| 80 | printk("allocation/free calls: %lu/%lu\n", dvma_allocs, dvma_frees); |
| 81 | printk("allocation/free bytes: %Lx/%Lx\n", dvma_alloc_bytes, |
| 82 | dvma_free_bytes); |
| 83 | } |
| 84 | |
| 85 | static void print_holes(struct list_head *holes) |
| 86 | { |
| 87 | |
| 88 | struct list_head *cur; |
| 89 | struct hole *hole; |
| 90 | |
| 91 | printk("listing dvma holes\n"); |
| 92 | list_for_each(cur, holes) { |
| 93 | hole = list_entry(cur, struct hole, list); |
| 94 | |
| 95 | if((hole->start == 0) && (hole->end == 0) && (hole->size == 0)) |
| 96 | continue; |
| 97 | |
| 98 | printk("hole: start %08lx end %08lx size %08lx\n", hole->start, hole->end, hole->size); |
| 99 | } |
| 100 | |
| 101 | printk("end of hole listing...\n"); |
| 102 | |
| 103 | } |
| 104 | #endif /* DVMA_DEBUG */ |
| 105 | |
| 106 | static inline int refill(void) |
| 107 | { |
| 108 | |
| 109 | struct hole *hole; |
| 110 | struct hole *prev = NULL; |
| 111 | struct list_head *cur; |
| 112 | int ret = 0; |
| 113 | |
| 114 | list_for_each(cur, &hole_list) { |
| 115 | hole = list_entry(cur, struct hole, list); |
| 116 | |
| 117 | if(!prev) { |
| 118 | prev = hole; |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | if(hole->end == prev->start) { |
| 123 | hole->size += prev->size; |
| 124 | hole->end = prev->end; |
Akinobu Mita | a7addce | 2006-06-26 00:24:39 -0700 | [diff] [blame] | 125 | list_move(&(prev->list), &hole_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | ret++; |
| 127 | } |
| 128 | |
| 129 | } |
| 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | static inline struct hole *rmcache(void) |
| 135 | { |
| 136 | struct hole *ret; |
| 137 | |
| 138 | if(list_empty(&hole_cache)) { |
| 139 | if(!refill()) { |
| 140 | printk("out of dvma hole cache!\n"); |
| 141 | BUG(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | ret = list_entry(hole_cache.next, struct hole, list); |
| 146 | list_del(&(ret->list)); |
| 147 | |
| 148 | return ret; |
| 149 | |
| 150 | } |
| 151 | |
| 152 | static inline unsigned long get_baddr(int len, unsigned long align) |
| 153 | { |
| 154 | |
| 155 | struct list_head *cur; |
| 156 | struct hole *hole; |
| 157 | |
| 158 | if(list_empty(&hole_list)) { |
| 159 | #ifdef DVMA_DEBUG |
| 160 | printk("out of dvma holes! (printing hole cache)\n"); |
| 161 | print_holes(&hole_cache); |
| 162 | print_use(); |
| 163 | #endif |
| 164 | BUG(); |
| 165 | } |
| 166 | |
| 167 | list_for_each(cur, &hole_list) { |
| 168 | unsigned long newlen; |
| 169 | |
| 170 | hole = list_entry(cur, struct hole, list); |
| 171 | |
| 172 | if(align > DVMA_PAGE_SIZE) |
| 173 | newlen = len + ((hole->end - len) & (align-1)); |
| 174 | else |
| 175 | newlen = len; |
| 176 | |
| 177 | if(hole->size > newlen) { |
| 178 | hole->end -= newlen; |
| 179 | hole->size -= newlen; |
| 180 | dvma_entry_use(hole->end) = newlen; |
| 181 | #ifdef DVMA_DEBUG |
| 182 | dvma_allocs++; |
| 183 | dvma_alloc_bytes += newlen; |
| 184 | #endif |
| 185 | return hole->end; |
| 186 | } else if(hole->size == newlen) { |
Akinobu Mita | a7addce | 2006-06-26 00:24:39 -0700 | [diff] [blame] | 187 | list_move(&(hole->list), &hole_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | dvma_entry_use(hole->start) = newlen; |
| 189 | #ifdef DVMA_DEBUG |
| 190 | dvma_allocs++; |
| 191 | dvma_alloc_bytes += newlen; |
| 192 | #endif |
| 193 | return hole->start; |
| 194 | } |
| 195 | |
| 196 | } |
| 197 | |
| 198 | printk("unable to find dvma hole!\n"); |
| 199 | BUG(); |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static inline int free_baddr(unsigned long baddr) |
| 204 | { |
| 205 | |
| 206 | unsigned long len; |
| 207 | struct hole *hole; |
| 208 | struct list_head *cur; |
| 209 | unsigned long orig_baddr; |
| 210 | |
| 211 | orig_baddr = baddr; |
| 212 | len = dvma_entry_use(baddr); |
| 213 | dvma_entry_use(baddr) = 0; |
| 214 | baddr &= DVMA_PAGE_MASK; |
| 215 | dvma_unmap_iommu(baddr, len); |
| 216 | |
| 217 | #ifdef DVMA_DEBUG |
| 218 | dvma_frees++; |
| 219 | dvma_free_bytes += len; |
| 220 | #endif |
| 221 | |
| 222 | list_for_each(cur, &hole_list) { |
| 223 | hole = list_entry(cur, struct hole, list); |
| 224 | |
| 225 | if(hole->end == baddr) { |
| 226 | hole->end += len; |
| 227 | hole->size += len; |
| 228 | return 0; |
| 229 | } else if(hole->start == (baddr + len)) { |
| 230 | hole->start = baddr; |
| 231 | hole->size += len; |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | } |
| 236 | |
| 237 | hole = rmcache(); |
| 238 | |
| 239 | hole->start = baddr; |
| 240 | hole->end = baddr + len; |
| 241 | hole->size = len; |
| 242 | |
| 243 | // list_add_tail(&(hole->list), cur); |
| 244 | list_add(&(hole->list), cur); |
| 245 | |
| 246 | return 0; |
| 247 | |
| 248 | } |
| 249 | |
Geert Uytterhoeven | a4df02a | 2013-06-25 21:15:24 +0200 | [diff] [blame] | 250 | void __init dvma_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
| 252 | |
| 253 | struct hole *hole; |
| 254 | int i; |
| 255 | |
| 256 | INIT_LIST_HEAD(&hole_list); |
| 257 | INIT_LIST_HEAD(&hole_cache); |
| 258 | |
| 259 | /* prepare the hole cache */ |
| 260 | for(i = 0; i < 64; i++) |
| 261 | list_add(&(initholes[i].list), &hole_cache); |
| 262 | |
| 263 | hole = rmcache(); |
| 264 | hole->start = DVMA_START; |
| 265 | hole->end = DVMA_END; |
| 266 | hole->size = DVMA_SIZE; |
| 267 | |
| 268 | list_add(&(hole->list), &hole_list); |
| 269 | |
Geert Uytterhoeven | 7b3e8de | 2013-06-25 20:33:44 +0200 | [diff] [blame] | 270 | iommu_use = alloc_bootmem(IOMMU_TOTAL_ENTRIES * sizeof(unsigned long)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | dvma_unmap_iommu(DVMA_START, DVMA_SIZE); |
| 273 | |
| 274 | #ifdef CONFIG_SUN3 |
| 275 | sun3_dvma_init(); |
| 276 | #endif |
| 277 | |
| 278 | } |
| 279 | |
Denis Efremov | 5ecf85f | 2013-05-09 14:36:56 +0400 | [diff] [blame] | 280 | unsigned long dvma_map_align(unsigned long kaddr, int len, int align) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
| 282 | |
| 283 | unsigned long baddr; |
| 284 | unsigned long off; |
| 285 | |
| 286 | if(!len) |
| 287 | len = 0x800; |
| 288 | |
| 289 | if(!kaddr || !len) { |
| 290 | // printk("error: kaddr %lx len %x\n", kaddr, len); |
| 291 | // *(int *)4 = 0; |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | #ifdef DEBUG |
| 296 | printk("dvma_map request %08lx bytes from %08lx\n", |
| 297 | len, kaddr); |
| 298 | #endif |
| 299 | off = kaddr & ~DVMA_PAGE_MASK; |
| 300 | kaddr &= PAGE_MASK; |
| 301 | len += off; |
| 302 | len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK); |
| 303 | |
| 304 | if(align == 0) |
| 305 | align = DVMA_PAGE_SIZE; |
| 306 | else |
| 307 | align = ((align + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK); |
| 308 | |
| 309 | baddr = get_baddr(len, align); |
| 310 | // printk("using baddr %lx\n", baddr); |
| 311 | |
| 312 | if(!dvma_map_iommu(kaddr, baddr, len)) |
| 313 | return (baddr + off); |
| 314 | |
| 315 | printk("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr, baddr, len); |
| 316 | BUG(); |
| 317 | return 0; |
| 318 | } |
Al Viro | 2e81148 | 2006-10-11 17:28:27 +0100 | [diff] [blame] | 319 | EXPORT_SYMBOL(dvma_map_align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
| 321 | void dvma_unmap(void *baddr) |
| 322 | { |
| 323 | unsigned long addr; |
| 324 | |
| 325 | addr = (unsigned long)baddr; |
| 326 | /* check if this is a vme mapping */ |
| 327 | if(!(addr & 0x00f00000)) |
| 328 | addr |= 0xf00000; |
| 329 | |
| 330 | free_baddr(addr); |
| 331 | |
| 332 | return; |
| 333 | |
| 334 | } |
Al Viro | 2e81148 | 2006-10-11 17:28:27 +0100 | [diff] [blame] | 335 | EXPORT_SYMBOL(dvma_unmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | void *dvma_malloc_align(unsigned long len, unsigned long align) |
| 338 | { |
| 339 | unsigned long kaddr; |
| 340 | unsigned long baddr; |
| 341 | unsigned long vaddr; |
| 342 | |
| 343 | if(!len) |
| 344 | return NULL; |
| 345 | |
| 346 | #ifdef DEBUG |
| 347 | printk("dvma_malloc request %lx bytes\n", len); |
| 348 | #endif |
| 349 | len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK); |
| 350 | |
| 351 | if((kaddr = __get_free_pages(GFP_ATOMIC, get_order(len))) == 0) |
| 352 | return NULL; |
| 353 | |
| 354 | if((baddr = (unsigned long)dvma_map_align(kaddr, len, align)) == 0) { |
| 355 | free_pages(kaddr, get_order(len)); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | vaddr = dvma_btov(baddr); |
| 360 | |
| 361 | if(dvma_map_cpu(kaddr, vaddr, len) < 0) { |
| 362 | dvma_unmap((void *)baddr); |
| 363 | free_pages(kaddr, get_order(len)); |
| 364 | return NULL; |
| 365 | } |
| 366 | |
| 367 | #ifdef DEBUG |
| 368 | printk("mapped %08lx bytes %08lx kern -> %08lx bus\n", |
| 369 | len, kaddr, baddr); |
| 370 | #endif |
| 371 | |
| 372 | return (void *)vaddr; |
| 373 | |
| 374 | } |
Al Viro | 2e81148 | 2006-10-11 17:28:27 +0100 | [diff] [blame] | 375 | EXPORT_SYMBOL(dvma_malloc_align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
| 377 | void dvma_free(void *vaddr) |
| 378 | { |
| 379 | |
| 380 | return; |
| 381 | |
| 382 | } |
Al Viro | 2e81148 | 2006-10-11 17:28:27 +0100 | [diff] [blame] | 383 | EXPORT_SYMBOL(dvma_free); |