s5p_mfc_opr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * drivers/media/platform/s5p-mfc/s5p_mfc_opr.c
  3. *
  4. * Samsung MFC (Multi Function Codec - FIMV) driver
  5. * This file contains hw related functions.
  6. *
  7. * Kamil Debski, Copyright (c) 2012 Samsung Electronics Co., Ltd.
  8. * http://www.samsung.com/
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include "s5p_mfc_debug.h"
  15. #include "s5p_mfc_opr.h"
  16. #include "s5p_mfc_opr_v5.h"
  17. #include "s5p_mfc_opr_v6.h"
  18. static struct s5p_mfc_hw_ops *s5p_mfc_ops;
  19. void s5p_mfc_init_hw_ops(struct s5p_mfc_dev *dev)
  20. {
  21. if (IS_MFCV6_PLUS(dev)) {
  22. s5p_mfc_ops = s5p_mfc_init_hw_ops_v6();
  23. dev->warn_start = S5P_FIMV_ERR_WARNINGS_START_V6;
  24. } else {
  25. s5p_mfc_ops = s5p_mfc_init_hw_ops_v5();
  26. dev->warn_start = S5P_FIMV_ERR_WARNINGS_START;
  27. }
  28. dev->mfc_ops = s5p_mfc_ops;
  29. }
  30. void s5p_mfc_init_regs(struct s5p_mfc_dev *dev)
  31. {
  32. if (IS_MFCV6_PLUS(dev))
  33. dev->mfc_regs = s5p_mfc_init_regs_v6_plus(dev);
  34. }
  35. int s5p_mfc_alloc_priv_buf(struct s5p_mfc_dev *dev, unsigned int mem_ctx,
  36. struct s5p_mfc_priv_buf *b)
  37. {
  38. unsigned int bits = dev->mem_size >> PAGE_SHIFT;
  39. unsigned int count = b->size >> PAGE_SHIFT;
  40. unsigned int align = (SZ_64K >> PAGE_SHIFT) - 1;
  41. unsigned int start, offset;
  42. mfc_debug(3, "Allocating priv: %zu\n", b->size);
  43. if (dev->mem_virt) {
  44. start = bitmap_find_next_zero_area(dev->mem_bitmap, bits, 0, count, align);
  45. if (start > bits)
  46. goto no_mem;
  47. bitmap_set(dev->mem_bitmap, start, count);
  48. offset = start << PAGE_SHIFT;
  49. b->virt = dev->mem_virt + offset;
  50. b->dma = dev->mem_base + offset;
  51. } else {
  52. struct device *mem_dev = dev->mem_dev[mem_ctx];
  53. dma_addr_t base = dev->dma_base[mem_ctx];
  54. b->ctx = mem_ctx;
  55. b->virt = dma_alloc_coherent(mem_dev, b->size, &b->dma, GFP_KERNEL);
  56. if (!b->virt)
  57. goto no_mem;
  58. if (b->dma < base) {
  59. mfc_err("Invalid memory configuration - buffer (%pad) is below base memory address(%pad)\n",
  60. &b->dma, &base);
  61. dma_free_coherent(mem_dev, b->size, b->virt, b->dma);
  62. return -ENOMEM;
  63. }
  64. }
  65. mfc_debug(3, "Allocated addr %p %pad\n", b->virt, &b->dma);
  66. return 0;
  67. no_mem:
  68. mfc_err("Allocating private buffer of size %zu failed\n", b->size);
  69. return -ENOMEM;
  70. }
  71. int s5p_mfc_alloc_generic_buf(struct s5p_mfc_dev *dev, unsigned int mem_ctx,
  72. struct s5p_mfc_priv_buf *b)
  73. {
  74. struct device *mem_dev = dev->mem_dev[mem_ctx];
  75. mfc_debug(3, "Allocating generic buf: %zu\n", b->size);
  76. b->ctx = mem_ctx;
  77. b->virt = dma_alloc_coherent(mem_dev, b->size, &b->dma, GFP_KERNEL);
  78. if (!b->virt)
  79. goto no_mem;
  80. mfc_debug(3, "Allocated addr %p %pad\n", b->virt, &b->dma);
  81. return 0;
  82. no_mem:
  83. mfc_err("Allocating generic buffer of size %zu failed\n", b->size);
  84. return -ENOMEM;
  85. }
  86. void s5p_mfc_release_priv_buf(struct s5p_mfc_dev *dev,
  87. struct s5p_mfc_priv_buf *b)
  88. {
  89. if (dev->mem_virt) {
  90. unsigned int start = (b->dma - dev->mem_base) >> PAGE_SHIFT;
  91. unsigned int count = b->size >> PAGE_SHIFT;
  92. bitmap_clear(dev->mem_bitmap, start, count);
  93. } else {
  94. struct device *mem_dev = dev->mem_dev[b->ctx];
  95. dma_free_coherent(mem_dev, b->size, b->virt, b->dma);
  96. }
  97. b->virt = NULL;
  98. b->dma = 0;
  99. b->size = 0;
  100. }
  101. void s5p_mfc_release_generic_buf(struct s5p_mfc_dev *dev,
  102. struct s5p_mfc_priv_buf *b)
  103. {
  104. struct device *mem_dev = dev->mem_dev[b->ctx];
  105. dma_free_coherent(mem_dev, b->size, b->virt, b->dma);
  106. b->virt = NULL;
  107. b->dma = 0;
  108. b->size = 0;
  109. }