consistent.c 2.9 KB

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