bouncebuf.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Generic bounce buffer implementation
  3. *
  4. * Copyright (C) 2012 Marek Vasut <marex@denx.de>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <malloc.h>
  26. #include <errno.h>
  27. #include <bouncebuf.h>
  28. static int addr_aligned(struct bounce_buffer *state)
  29. {
  30. const ulong align_mask = ARCH_DMA_MINALIGN - 1;
  31. /* Check if start is aligned */
  32. if ((ulong)state->user_buffer & align_mask) {
  33. debug("Unaligned buffer address %p\n", state->user_buffer);
  34. return 0;
  35. }
  36. /* Check if length is aligned */
  37. if (state->len != state->len_aligned) {
  38. debug("Unaligned buffer length %d\n", state->len);
  39. return 0;
  40. }
  41. /* Aligned */
  42. return 1;
  43. }
  44. int bounce_buffer_start(struct bounce_buffer *state, void *data,
  45. size_t len, unsigned int flags)
  46. {
  47. state->user_buffer = data;
  48. state->bounce_buffer = data;
  49. state->len = len;
  50. state->len_aligned = roundup(len, ARCH_DMA_MINALIGN);
  51. state->flags = flags;
  52. if (!addr_aligned(state)) {
  53. state->bounce_buffer = memalign(ARCH_DMA_MINALIGN,
  54. state->len_aligned);
  55. if (!state->bounce_buffer)
  56. return -ENOMEM;
  57. if (state->flags & GEN_BB_READ)
  58. memcpy(state->bounce_buffer, state->user_buffer,
  59. state->len);
  60. }
  61. /*
  62. * Flush data to RAM so DMA reads can pick it up,
  63. * and any CPU writebacks don't race with DMA writes
  64. */
  65. flush_dcache_range((unsigned long)state->bounce_buffer,
  66. (unsigned long)(state->bounce_buffer) +
  67. state->len_aligned);
  68. return 0;
  69. }
  70. int bounce_buffer_stop(struct bounce_buffer *state)
  71. {
  72. if (state->flags & GEN_BB_WRITE) {
  73. /* Invalidate cache so that CPU can see any newly DMA'd data */
  74. invalidate_dcache_range((unsigned long)state->bounce_buffer,
  75. (unsigned long)(state->bounce_buffer) +
  76. state->len_aligned);
  77. }
  78. if (state->bounce_buffer == state->user_buffer)
  79. return 0;
  80. if (state->flags & GEN_BB_WRITE)
  81. memcpy(state->user_buffer, state->bounce_buffer, state->len);
  82. free(state->bounce_buffer);
  83. return 0;
  84. }