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