consistent.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. struct voya_alloc_entry {
  19. struct list_head list;
  20. unsigned long ofs;
  21. unsigned long len;
  22. };
  23. static DEFINE_SPINLOCK(voya_list_lock);
  24. static LIST_HEAD(voya_alloc_list);
  25. #define OHCI_SRAM_START 0xb0000000
  26. #define OHCI_HCCA_SIZE 0x100
  27. #define OHCI_SRAM_SIZE 0x10000
  28. #define VOYAGER_OHCI_NAME "voyager-ohci"
  29. void *voyagergx_consistent_alloc(struct device *dev, size_t size,
  30. dma_addr_t *handle, gfp_t flag)
  31. {
  32. struct list_head *list = &voya_alloc_list;
  33. struct voya_alloc_entry *entry;
  34. unsigned long start, end;
  35. unsigned long flags;
  36. /*
  37. * The SM501 contains an integrated 8051 with its own SRAM.
  38. * Devices within the cchip can all hook into the 8051 SRAM.
  39. * We presently use this for the OHCI.
  40. *
  41. * Everything else goes through consistent_alloc().
  42. */
  43. if (!dev || strcmp(dev->driver->name, VOYAGER_OHCI_NAME))
  44. return NULL;
  45. start = OHCI_SRAM_START + OHCI_HCCA_SIZE;
  46. entry = kmalloc(sizeof(struct voya_alloc_entry), GFP_ATOMIC);
  47. if (!entry)
  48. return ERR_PTR(-ENOMEM);
  49. entry->len = (size + 15) & ~15;
  50. /*
  51. * The basis for this allocator is dwmw2's malloc.. the
  52. * Matrox allocator :-)
  53. */
  54. spin_lock_irqsave(&voya_list_lock, flags);
  55. list_for_each(list, &voya_alloc_list) {
  56. struct voya_alloc_entry *p;
  57. p = list_entry(list, struct voya_alloc_entry, list);
  58. if (p->ofs - start >= size)
  59. goto out;
  60. start = p->ofs + p->len;
  61. }
  62. end = start + (OHCI_SRAM_SIZE - OHCI_HCCA_SIZE);
  63. list = &voya_alloc_list;
  64. if (end - start >= size) {
  65. out:
  66. entry->ofs = start;
  67. list_add_tail(&entry->list, list);
  68. spin_unlock_irqrestore(&voya_list_lock, flags);
  69. *handle = start;
  70. return (void *)start;
  71. }
  72. kfree(entry);
  73. spin_unlock_irqrestore(&voya_list_lock, flags);
  74. return ERR_PTR(-EINVAL);
  75. }
  76. int voyagergx_consistent_free(struct device *dev, size_t size,
  77. void *vaddr, dma_addr_t handle)
  78. {
  79. struct voya_alloc_entry *entry;
  80. unsigned long flags;
  81. if (!dev || strcmp(dev->driver->name, VOYAGER_OHCI_NAME))
  82. return -EINVAL;
  83. spin_lock_irqsave(&voya_list_lock, flags);
  84. list_for_each_entry(entry, &voya_alloc_list, list) {
  85. if (entry->ofs != handle)
  86. continue;
  87. list_del(&entry->list);
  88. kfree(entry);
  89. break;
  90. }
  91. spin_unlock_irqrestore(&voya_list_lock, flags);
  92. return 0;
  93. }
  94. EXPORT_SYMBOL(voyagergx_consistent_alloc);
  95. EXPORT_SYMBOL(voyagergx_consistent_free);