testbench_memalloc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2014 - 2021 VERISILICON
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. *****************************************************************************
  26. *
  27. * The GPL License (GPL)
  28. *
  29. * Copyright (C) 2014 - 2021 VERISILICON
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version 2
  34. * of the License, or (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program; if not, write to the Free Software Foundation,
  43. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  44. *
  45. *****************************************************************************
  46. *
  47. * Note: This software is released under dual MIT and GPL licenses. A
  48. * recipient may use this file under the terms of either the MIT license or
  49. * GPL License. If you wish to use only one license not the other, you can
  50. * indicate your decision by deleting one of the above license notices in your
  51. * version of this file.
  52. *
  53. *****************************************************************************/
  54. #include <sys/types.h>
  55. #include <sys/mman.h>
  56. #include <sys/ioctl.h>
  57. #include <unistd.h>
  58. #include <fcntl.h>
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <getopt.h>
  63. #include <assert.h>
  64. #include "basetype.h"
  65. #include "memalloc.h"
  66. int main(void) {
  67. i32 memdev_fd = -1;
  68. i32 memdev_fd_2 = -1;
  69. i32 memdev_map = -1;
  70. i32 i = 0, u = 0;
  71. u32 *virtual1 = MAP_FAILED;
  72. u32 *virtual2 = MAP_FAILED;
  73. u32 pgsize = getpagesize();
  74. const char *memdev = "/dev/memalloc";
  75. const char *memmap = "/dev/mem";
  76. u32 bus_address1 = 0, bus_address2 = 0;
  77. u32 size = 4096;
  78. memdev_fd = open(memdev, O_RDWR);
  79. if (memdev_fd == -1) {
  80. printf("Failed to open dev: %s\n", memdev);
  81. goto end1;
  82. }
  83. memdev_fd_2 = open(memdev, O_RDWR);
  84. if (memdev_fd_2 == -1) {
  85. printf("Failed to open dev: %s\n", memdev);
  86. goto end1;
  87. }
  88. memdev_map = open(memmap, O_RDWR);
  89. if (memdev_map == -1) {
  90. printf("Failed to open dev: %s\n", memmap);
  91. goto end1;
  92. }
  93. size = (size + pgsize) & (~(pgsize - 1));
  94. printf("Hard Reset\n", size);
  95. /*ioctl(memdev_fd, MEMALLOC_IOCHARDRESET, 0);*/
  96. for (i = 0; i < 100; i++) {
  97. ioctl(memdev_fd, MEMALLOC_IOCXGETBUFFER, &bus_address1);
  98. printf("bus_address1 0x%08x\n", bus_address1);
  99. ioctl(memdev_fd_2, MEMALLOC_IOCXGETBUFFER, &bus_address2);
  100. printf("bus_address2 0x%08x\n", bus_address2);
  101. /* test write stuff in the mem are*/
  102. if (bus_address1) {
  103. virtual1 = (u32 *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED,
  104. memdev_map, bus_address1);
  105. printf("Virtual1 %08x\n", virtual1);
  106. }
  107. if (virtual1 != MAP_FAILED) {
  108. for (u = 0; u < size / 4; u++) {
  109. *virtual1 = i + 1;
  110. }
  111. } else {
  112. printf("map failed\n");
  113. }
  114. /* test write stuff in the mem are*/
  115. if (bus_address2) {
  116. virtual2 = (u32 *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED,
  117. memdev_map, bus_address2);
  118. printf("Virtual2 0x%08x\n", virtual2);
  119. }
  120. if (virtual2 != MAP_FAILED) {
  121. for (u = 0; u < size / 4; u++) {
  122. *virtual2 = i + 2;
  123. }
  124. } else {
  125. printf("map failed\n");
  126. }
  127. if (virtual1 != MAP_FAILED) {
  128. for (u = 0; u < size / 4; u++) {
  129. if (*virtual1 != i + 1) {
  130. printf("MISMATCH1!\n");
  131. break;
  132. }
  133. }
  134. }
  135. if (virtual2 != MAP_FAILED) {
  136. for (u = 0; u < size / 4; u++) {
  137. if (*virtual2 != i + 2) {
  138. printf("MISMATCH2!\n");
  139. break;
  140. }
  141. }
  142. }
  143. if ((i % 30)) {
  144. printf("release %d ", size);
  145. ioctl(memdev_fd, MEMALLOC_IOCSFREEBUFFER, &bus_address1);
  146. printf("address:\t\t0x%08x\n", bus_address1);
  147. }
  148. printf("release %d ", size);
  149. /*ioctl(memdev_fd_2, MEMALLOC_IOCSFREEBUFFER, &bus_address2);
  150. printf("address:\t\t0x%08x\n", bus_address2);
  151. virtual1 = MAP_FAILED;
  152. virtual2 = MAP_FAILED;*/
  153. if (!(i % 30)) {
  154. close(memdev_fd);
  155. memdev_fd = open(memdev, O_RDWR);
  156. if (memdev_fd == -1) {
  157. printf("Failed to open dev: %s\n", memdev);
  158. goto end1;
  159. }
  160. }
  161. close(memdev_fd_2);
  162. memdev_fd_2 = open(memdev, O_RDWR);
  163. if (memdev_fd_2 == -1) {
  164. printf("Failed to open dev: %s\n", memdev);
  165. goto end1;
  166. }
  167. }
  168. end1:
  169. close(memdev_fd);
  170. close(memdev_fd_2);
  171. close(memdev_map);
  172. return 0;
  173. }