hwaccess.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <sys/types.h>
  24. #if !defined (__DJGPP__) && !defined(__LIBPAYLOAD__)
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #endif
  28. #if !defined (__DJGPP__)
  29. #include <errno.h>
  30. #endif
  31. #include "flash.h"
  32. #if defined(__i386__) || defined(__x86_64__)
  33. /* sync primitive is not needed because x86 uses uncached accesses
  34. * which have a strongly ordered memory model.
  35. */
  36. static inline void sync_primitive(void)
  37. {
  38. }
  39. #if defined(__FreeBSD__) || defined(__DragonFly__)
  40. int io_fd;
  41. #endif
  42. void get_io_perms(void)
  43. {
  44. #if defined(__DJGPP__) || defined(__LIBPAYLOAD__)
  45. /* We have full permissions by default. */
  46. return;
  47. #else
  48. #if defined (__sun) && (defined(__i386) || defined(__amd64))
  49. if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
  50. #elif defined(__FreeBSD__) || defined (__DragonFly__)
  51. if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
  52. #else
  53. if (iopl(3) != 0) {
  54. #endif
  55. msg_perr("ERROR: Could not get I/O privileges (%s).\n"
  56. "You need to be root.\n", strerror(errno));
  57. #if defined (__OpenBSD__)
  58. msg_perr("Please set securelevel=-1 in /etc/rc.securelevel "
  59. "and reboot, or reboot into \n");
  60. msg_perr("single user mode.\n");
  61. #endif
  62. exit(1);
  63. }
  64. #endif
  65. }
  66. void release_io_perms(void)
  67. {
  68. #if defined(__FreeBSD__) || defined(__DragonFly__)
  69. close(io_fd);
  70. #endif
  71. }
  72. #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__)
  73. static inline void sync_primitive(void)
  74. {
  75. /* Prevent reordering and/or merging of reads/writes to hardware.
  76. * Such reordering and/or merging would break device accesses which
  77. * depend on the exact access order.
  78. */
  79. asm("eieio" : : : "memory");
  80. }
  81. /* PCI port I/O is not yet implemented on PowerPC. */
  82. void get_io_perms(void)
  83. {
  84. }
  85. /* PCI port I/O is not yet implemented on PowerPC. */
  86. void release_io_perms(void)
  87. {
  88. }
  89. #elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips)
  90. /* sync primitive is not needed because /dev/mem on MIPS uses uncached accesses
  91. * in mode 2 which has a strongly ordered memory model.
  92. */
  93. static inline void sync_primitive(void)
  94. {
  95. }
  96. /* PCI port I/O is not yet implemented on MIPS. */
  97. void get_io_perms(void)
  98. {
  99. }
  100. /* PCI port I/O is not yet implemented on MIPS. */
  101. void release_io_perms(void)
  102. {
  103. }
  104. #elif defined (__arm__)
  105. static inline void sync_primitive(void)
  106. {
  107. }
  108. void get_io_perms(void)
  109. {
  110. }
  111. void release_io_perms(void)
  112. {
  113. }
  114. #else
  115. #error Unknown architecture
  116. #endif
  117. void mmio_writeb(uint8_t val, void *addr)
  118. {
  119. *(volatile uint8_t *) addr = val;
  120. sync_primitive();
  121. }
  122. void mmio_writew(uint16_t val, void *addr)
  123. {
  124. *(volatile uint16_t *) addr = val;
  125. sync_primitive();
  126. }
  127. void mmio_writel(uint32_t val, void *addr)
  128. {
  129. *(volatile uint32_t *) addr = val;
  130. sync_primitive();
  131. }
  132. uint8_t mmio_readb(void *addr)
  133. {
  134. return *(volatile uint8_t *) addr;
  135. }
  136. uint16_t mmio_readw(void *addr)
  137. {
  138. return *(volatile uint16_t *) addr;
  139. }
  140. uint32_t mmio_readl(void *addr)
  141. {
  142. return *(volatile uint32_t *) addr;
  143. }
  144. void mmio_le_writeb(uint8_t val, void *addr)
  145. {
  146. mmio_writeb(cpu_to_le8(val), addr);
  147. }
  148. void mmio_le_writew(uint16_t val, void *addr)
  149. {
  150. mmio_writew(cpu_to_le16(val), addr);
  151. }
  152. void mmio_le_writel(uint32_t val, void *addr)
  153. {
  154. mmio_writel(cpu_to_le32(val), addr);
  155. }
  156. uint8_t mmio_le_readb(void *addr)
  157. {
  158. return le_to_cpu8(mmio_readb(addr));
  159. }
  160. uint16_t mmio_le_readw(void *addr)
  161. {
  162. return le_to_cpu16(mmio_readw(addr));
  163. }
  164. uint32_t mmio_le_readl(void *addr)
  165. {
  166. return le_to_cpu32(mmio_readl(addr));
  167. }
  168. enum mmio_write_type {
  169. mmio_write_type_b,
  170. mmio_write_type_w,
  171. mmio_write_type_l,
  172. };
  173. struct undo_mmio_write_data {
  174. void *addr;
  175. int reg;
  176. enum mmio_write_type type;
  177. union {
  178. uint8_t bdata;
  179. uint16_t wdata;
  180. uint32_t ldata;
  181. };
  182. };
  183. int undo_mmio_write(void *p)
  184. {
  185. struct undo_mmio_write_data *data = p;
  186. msg_pdbg("Restoring MMIO space at %p\n", data->addr);
  187. switch (data->type) {
  188. case mmio_write_type_b:
  189. mmio_writeb(data->bdata, data->addr);
  190. break;
  191. case mmio_write_type_w:
  192. mmio_writew(data->wdata, data->addr);
  193. break;
  194. case mmio_write_type_l:
  195. mmio_writel(data->ldata, data->addr);
  196. break;
  197. }
  198. /* p was allocated in register_undo_mmio_write. */
  199. free(p);
  200. return 0;
  201. }
  202. #define register_undo_mmio_write(a, c) \
  203. { \
  204. struct undo_mmio_write_data *undo_mmio_write_data; \
  205. undo_mmio_write_data = malloc(sizeof(struct undo_mmio_write_data)); \
  206. if (!undo_mmio_write_data) { \
  207. msg_gerr("Out of memory!\n"); \
  208. exit(1); \
  209. } \
  210. undo_mmio_write_data->addr = a; \
  211. undo_mmio_write_data->type = mmio_write_type_##c; \
  212. undo_mmio_write_data->c##data = mmio_read##c(a); \
  213. register_shutdown(undo_mmio_write, undo_mmio_write_data); \
  214. }
  215. #define register_undo_mmio_writeb(a) register_undo_mmio_write(a, b)
  216. #define register_undo_mmio_writew(a) register_undo_mmio_write(a, w)
  217. #define register_undo_mmio_writel(a) register_undo_mmio_write(a, l)
  218. void rmmio_writeb(uint8_t val, void *addr)
  219. {
  220. register_undo_mmio_writeb(addr);
  221. mmio_writeb(val, addr);
  222. }
  223. void rmmio_writew(uint16_t val, void *addr)
  224. {
  225. register_undo_mmio_writew(addr);
  226. mmio_writew(val, addr);
  227. }
  228. void rmmio_writel(uint32_t val, void *addr)
  229. {
  230. register_undo_mmio_writel(addr);
  231. mmio_writel(val, addr);
  232. }
  233. void rmmio_le_writeb(uint8_t val, void *addr)
  234. {
  235. register_undo_mmio_writeb(addr);
  236. mmio_le_writeb(val, addr);
  237. }
  238. void rmmio_le_writew(uint16_t val, void *addr)
  239. {
  240. register_undo_mmio_writew(addr);
  241. mmio_le_writew(val, addr);
  242. }
  243. void rmmio_le_writel(uint32_t val, void *addr)
  244. {
  245. register_undo_mmio_writel(addr);
  246. mmio_le_writel(val, addr);
  247. }
  248. void rmmio_valb(void *addr)
  249. {
  250. register_undo_mmio_writeb(addr);
  251. }
  252. void rmmio_valw(void *addr)
  253. {
  254. register_undo_mmio_writew(addr);
  255. }
  256. void rmmio_vall(void *addr)
  257. {
  258. register_undo_mmio_writel(addr);
  259. }