consistent.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * arch/sh/mm/consistent.c
  3. *
  4. * Copyright (C) 2004 - 2007 Paul Mundt
  5. *
  6. * Declared coherent memory functions based on arch/x86/kernel/pci-dma_32.c
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/mm.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/dma-mapping.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/addrspace.h>
  17. #include <asm/io.h>
  18. void *dma_alloc_coherent(struct device *dev, size_t size,
  19. dma_addr_t *dma_handle, gfp_t gfp)
  20. {
  21. void *ret, *ret_nocache;
  22. int order = get_order(size);
  23. if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
  24. return ret;
  25. ret = (void *)__get_free_pages(gfp, order);
  26. if (!ret)
  27. return NULL;
  28. memset(ret, 0, size);
  29. /*
  30. * Pages from the page allocator may have data present in
  31. * cache. So flush the cache before using uncached memory.
  32. */
  33. dma_cache_sync(dev, ret, size, DMA_BIDIRECTIONAL);
  34. ret_nocache = (void __force *)ioremap_nocache(virt_to_phys(ret), size);
  35. if (!ret_nocache) {
  36. free_pages((unsigned long)ret, order);
  37. return NULL;
  38. }
  39. split_page(pfn_to_page(virt_to_phys(ret) >> PAGE_SHIFT), order);
  40. *dma_handle = virt_to_phys(ret);
  41. return ret_nocache;
  42. }
  43. EXPORT_SYMBOL(dma_alloc_coherent);
  44. void dma_free_coherent(struct device *dev, size_t size,
  45. void *vaddr, dma_addr_t dma_handle)
  46. {
  47. int order = get_order(size);
  48. unsigned long pfn = dma_handle >> PAGE_SHIFT;
  49. int k;
  50. if (!dma_release_from_coherent(dev, order, vaddr)) {
  51. WARN_ON(irqs_disabled()); /* for portability */
  52. for (k = 0; k < (1 << order); k++)
  53. __free_pages(pfn_to_page(pfn + k), 0);
  54. iounmap(vaddr);
  55. }
  56. }
  57. EXPORT_SYMBOL(dma_free_coherent);
  58. void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  59. enum dma_data_direction direction)
  60. {
  61. #ifdef CONFIG_CPU_SH5
  62. void *p1addr = vaddr;
  63. #else
  64. void *p1addr = (void*) P1SEGADDR((unsigned long)vaddr);
  65. #endif
  66. switch (direction) {
  67. case DMA_FROM_DEVICE: /* invalidate only */
  68. __flush_invalidate_region(p1addr, size);
  69. break;
  70. case DMA_TO_DEVICE: /* writeback only */
  71. __flush_wback_region(p1addr, size);
  72. break;
  73. case DMA_BIDIRECTIONAL: /* writeback and invalidate */
  74. __flush_purge_region(p1addr, size);
  75. break;
  76. default:
  77. BUG();
  78. }
  79. }
  80. EXPORT_SYMBOL(dma_cache_sync);
  81. static int __init memchunk_setup(char *str)
  82. {
  83. return 1; /* accept anything that begins with "memchunk." */
  84. }
  85. __setup("memchunk.", memchunk_setup);
  86. static void __init memchunk_cmdline_override(char *name, unsigned long *sizep)
  87. {
  88. char *p = boot_command_line;
  89. int k = strlen(name);
  90. while ((p = strstr(p, "memchunk."))) {
  91. p += 9; /* strlen("memchunk.") */
  92. if (!strncmp(name, p, k) && p[k] == '=') {
  93. p += k + 1;
  94. *sizep = memparse(p, NULL);
  95. pr_info("%s: forcing memory chunk size to 0x%08lx\n",
  96. name, *sizep);
  97. break;
  98. }
  99. }
  100. }
  101. int __init platform_resource_setup_memory(struct platform_device *pdev,
  102. char *name, unsigned long memsize)
  103. {
  104. struct resource *r;
  105. dma_addr_t dma_handle;
  106. void *buf;
  107. r = pdev->resource + pdev->num_resources - 1;
  108. if (r->flags) {
  109. pr_warning("%s: unable to find empty space for resource\n",
  110. name);
  111. return -EINVAL;
  112. }
  113. memchunk_cmdline_override(name, &memsize);
  114. if (!memsize)
  115. return 0;
  116. buf = dma_alloc_coherent(NULL, memsize, &dma_handle, GFP_KERNEL);
  117. if (!buf) {
  118. pr_warning("%s: unable to allocate memory\n", name);
  119. return -ENOMEM;
  120. }
  121. memset(buf, 0, memsize);
  122. r->flags = IORESOURCE_MEM;
  123. r->start = dma_handle;
  124. r->end = r->start + memsize - 1;
  125. r->name = name;
  126. return 0;
  127. }