1/* 2 * arch/sh/cchips/voyagergx/consistent.c 3 * 4 * Copyright (C) 2004 Paul Mundt 5 * 6 * This file is subject to the terms and conditions of the GNU General Public 7 * License. See the file "COPYING" in the main directory of this archive 8 * for more details. 9 */ 10#include <linux/mm.h> 11#include <linux/dma-mapping.h> 12#include <linux/slab.h> 13#include <linux/list.h> 14#include <linux/types.h> 15#include <linux/module.h> 16#include <linux/device.h> 17#include <asm/io.h> 18 19 20struct voya_alloc_entry { 21 struct list_head list; 22 unsigned long ofs; 23 unsigned long len; 24}; 25 26static DEFINE_SPINLOCK(voya_list_lock); 27static LIST_HEAD(voya_alloc_list); 28 29#define OHCI_SRAM_START 0xb0000000 30#define OHCI_HCCA_SIZE 0x100 31#define OHCI_SRAM_SIZE 0x10000 32 33#define VOYAGER_OHCI_NAME "voyager-ohci" 34 35void *voyagergx_consistent_alloc(struct device *dev, size_t size, 36 dma_addr_t *handle, gfp_t flag) 37{ 38 struct list_head *list = &voya_alloc_list; 39 struct voya_alloc_entry *entry; 40 unsigned long start, end; 41 unsigned long flags; 42 43 /* 44 * The SM501 contains an integrated 8051 with its own SRAM. 45 * Devices within the cchip can all hook into the 8051 SRAM. 46 * We presently use this for the OHCI. 47 * 48 * Everything else goes through consistent_alloc(). 49 */ 50 if (!dev || strcmp(dev->driver->name, VOYAGER_OHCI_NAME)) 51 return NULL; 52 53 start = OHCI_SRAM_START + OHCI_HCCA_SIZE; 54 55 entry = kmalloc(sizeof(struct voya_alloc_entry), GFP_ATOMIC); 56 if (!entry) 57 return ERR_PTR(-ENOMEM); 58 59 entry->len = (size + 15) & ~15; 60 61 /* 62 * The basis for this allocator is dwmw2's malloc.. the 63 * Matrox allocator :-) 64 */ 65 spin_lock_irqsave(&voya_list_lock, flags); 66 list_for_each(list, &voya_alloc_list) { 67 struct voya_alloc_entry *p; 68 69 p = list_entry(list, struct voya_alloc_entry, list); 70 71 if (p->ofs - start >= size) 72 goto out; 73 74 start = p->ofs + p->len; 75 } 76 77 end = start + (OHCI_SRAM_SIZE - OHCI_HCCA_SIZE); 78 list = &voya_alloc_list; 79 80 if (end - start >= size) { 81out: 82 entry->ofs = start; 83 list_add_tail(&entry->list, list); 84 spin_unlock_irqrestore(&voya_list_lock, flags); 85 86 *handle = start; 87 return (void *)start; 88 } 89 90 kfree(entry); 91 spin_unlock_irqrestore(&voya_list_lock, flags); 92 93 return ERR_PTR(-EINVAL); 94} 95 96int voyagergx_consistent_free(struct device *dev, size_t size, 97 void *vaddr, dma_addr_t handle) 98{ 99 struct voya_alloc_entry *entry; 100 unsigned long flags; 101 102 if (!dev || strcmp(dev->driver->name, VOYAGER_OHCI_NAME)) 103 return -EINVAL; 104 105 spin_lock_irqsave(&voya_list_lock, flags); 106 list_for_each_entry(entry, &voya_alloc_list, list) { 107 if (entry->ofs != handle) 108 continue; 109 110 list_del(&entry->list); 111 kfree(entry); 112 113 break; 114 } 115 spin_unlock_irqrestore(&voya_list_lock, flags); 116 117 return 0; 118} 119 120EXPORT_SYMBOL(voyagergx_consistent_alloc); 121EXPORT_SYMBOL(voyagergx_consistent_free); 122