hw_shm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* This file is part of the program psim.
  2. Copyright (C) 1997,2008, Joel Sherrill <joel@OARcorp.com>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _HW_SHM_C_
  15. #define _HW_SHM_C_
  16. #include "device_table.h"
  17. #include <string.h>
  18. #include <sys/ipc.h>
  19. #include <sys/shm.h>
  20. /* DEVICE
  21. shm - map unix shared memory into psim address space
  22. DESCRIPTION
  23. This device implements an area of memory which is mapped into UNIX
  24. shared memory.
  25. PROPERTIES
  26. reg = <address> <size> (required)
  27. Determine where the memory lives in the parents address space.
  28. The SHM area is assumed to be of the same length.
  29. key = <integer> (required)
  30. This is the key of the unix shared memory area.
  31. EXAMPLES
  32. Enable tracing of the shm:
  33. | bash$ psim -t shm-device \
  34. Configure a 512 kilobytes of UNIX shared memory with the key 0x12345678
  35. mapped into psim address space at 0x0c000000.
  36. | -o '/shm@0x0c000000/reg 0x0c000000 0x80000' \
  37. | -o '/shm@0x0c000000/key 0x12345678' \
  38. sim/ppc/run -o '/#address-cells 1' \
  39. -o '/shm@0x0c000000/reg 0x0c000000 0x80000' \
  40. -o '/shm@0x0c000000/key 0x12345678' ../psim-hello/hello
  41. BUGS
  42. None known.
  43. */
  44. typedef struct _hw_shm_device {
  45. unsigned_word physical_address;
  46. char *shm_address;
  47. unsigned sizeof_memory;
  48. key_t key;
  49. int id;
  50. } hw_shm_device;
  51. static void
  52. hw_shm_init_data(device *me)
  53. {
  54. hw_shm_device *shm = (hw_shm_device*)device_data(me);
  55. reg_property_spec reg;
  56. int i;
  57. /* Obtain the Key Value */
  58. if (device_find_property(me, "key") == NULL)
  59. error("shm_init_data() required key property is missing\n");
  60. shm->key = (key_t) device_find_integer_property(me, "key");
  61. DTRACE(shm, ("shm key (0x%08x)\n", shm->key) );
  62. /* Figure out where this memory is in address space and how long it is */
  63. if ( !device_find_reg_array_property(me, "reg", 0, &reg) )
  64. error("hw_shm_init_data() no address registered\n");
  65. /* Determine the address and length being as paranoid as possible */
  66. shm->physical_address = 0xffffffff;
  67. shm->sizeof_memory = 0xffffffff;
  68. for ( i=0 ; i<reg.address.nr_cells; i++ ) {
  69. if (reg.address.cells[0] == 0 && reg.size.cells[0] == 0)
  70. continue;
  71. if ( shm->physical_address != 0xffffffff )
  72. device_error(me, "Only single celled address ranges supported\n");
  73. shm->physical_address = reg.address.cells[i];
  74. DTRACE(shm, ("shm physical_address=0x%x\n", shm->physical_address));
  75. shm->sizeof_memory = reg.size.cells[i];
  76. DTRACE(shm, ("shm length=0x%x\n", shm->sizeof_memory));
  77. }
  78. if ( shm->physical_address == 0xffffffff )
  79. device_error(me, "Address not specified\n" );
  80. if ( shm->sizeof_memory == 0xffffffff )
  81. device_error(me, "Length not specified\n" );
  82. /* Now actually attach to or create the shared memory area */
  83. shm->id = shmget(shm->key, shm->sizeof_memory, IPC_CREAT | 0660);
  84. if (shm->id == -1)
  85. error("hw_shm_init_data() shmget failed\n");
  86. shm->shm_address = shmat(shm->id, (char *)0, SHM_RND);
  87. if (shm->shm_address == (void *)-1)
  88. error("hw_shm_init_data() shmat failed\n");
  89. }
  90. static void
  91. hw_shm_attach_address_callback(device *me,
  92. attach_type attach,
  93. int space,
  94. unsigned_word addr,
  95. unsigned nr_bytes,
  96. access_type access,
  97. device *client) /*callback/default*/
  98. {
  99. if (space != 0)
  100. error("shm_attach_address_callback() invalid address space\n");
  101. if (nr_bytes == 0)
  102. error("shm_attach_address_callback() invalid size\n");
  103. }
  104. static unsigned
  105. hw_shm_io_read_buffer(device *me,
  106. void *dest,
  107. int space,
  108. unsigned_word addr,
  109. unsigned nr_bytes,
  110. cpu *processor,
  111. unsigned_word cia)
  112. {
  113. hw_shm_device *shm = (hw_shm_device*)device_data(me);
  114. /* do we need to worry about out of range addresses? */
  115. DTRACE(shm, ("read %p %x %x %x\n", \
  116. shm->shm_address, shm->physical_address, addr, nr_bytes) );
  117. memcpy(dest, &shm->shm_address[addr - shm->physical_address], nr_bytes);
  118. return nr_bytes;
  119. }
  120. static unsigned
  121. hw_shm_io_write_buffer(device *me,
  122. const void *source,
  123. int space,
  124. unsigned_word addr,
  125. unsigned nr_bytes,
  126. cpu *processor,
  127. unsigned_word cia)
  128. {
  129. hw_shm_device *shm = (hw_shm_device*)device_data(me);
  130. /* do we need to worry about out of range addresses? */
  131. DTRACE(shm, ("write %p %x %x %x\n", \
  132. shm->shm_address, shm->physical_address, addr, nr_bytes) );
  133. memcpy(&shm->shm_address[addr - shm->physical_address], source, nr_bytes);
  134. return nr_bytes;
  135. }
  136. static device_callbacks const hw_shm_callbacks = {
  137. { generic_device_init_address, hw_shm_init_data },
  138. { hw_shm_attach_address_callback, }, /* address */
  139. { hw_shm_io_read_buffer,
  140. hw_shm_io_write_buffer }, /* IO */
  141. { NULL, }, /* DMA */
  142. { NULL, }, /* interrupt */
  143. { NULL, }, /* unit */
  144. NULL,
  145. };
  146. static void *
  147. hw_shm_create(const char *name,
  148. const device_unit *unit_address,
  149. const char *args)
  150. {
  151. hw_shm_device *shm = ZALLOC(hw_shm_device);
  152. return shm;
  153. }
  154. const device_descriptor hw_shm_device_descriptor[] = {
  155. { "shm", hw_shm_create, &hw_shm_callbacks },
  156. { NULL },
  157. };
  158. #endif /* _HW_SHM_C_ */