2
0

ioport.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /*
  25. * splitted out ioport related stuffs from vl.c.
  26. */
  27. #include "exec/ioport.h"
  28. #include "trace.h"
  29. #include "exec/memory.h"
  30. /***********************************************************/
  31. /* IO Port */
  32. //#define DEBUG_UNUSED_IOPORT
  33. //#define DEBUG_IOPORT
  34. #ifdef DEBUG_UNUSED_IOPORT
  35. # define LOG_UNUSED_IOPORT(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
  36. #else
  37. # define LOG_UNUSED_IOPORT(fmt, ...) do{ } while (0)
  38. #endif
  39. #ifdef DEBUG_IOPORT
  40. # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
  41. #else
  42. # define LOG_IOPORT(...) do { } while (0)
  43. #endif
  44. /* XXX: use a two level table to limit memory usage */
  45. static void *ioport_opaque[MAX_IOPORTS];
  46. static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
  47. static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
  48. static IOPortDestructor *ioport_destructor_table[MAX_IOPORTS];
  49. static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
  50. static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
  51. static uint32_t ioport_read(int index, uint32_t address)
  52. {
  53. static IOPortReadFunc * const default_func[3] = {
  54. default_ioport_readb,
  55. default_ioport_readw,
  56. default_ioport_readl
  57. };
  58. IOPortReadFunc *func = ioport_read_table[index][address];
  59. if (!func)
  60. func = default_func[index];
  61. return func(ioport_opaque[address], address);
  62. }
  63. static void ioport_write(int index, uint32_t address, uint32_t data)
  64. {
  65. static IOPortWriteFunc * const default_func[3] = {
  66. default_ioport_writeb,
  67. default_ioport_writew,
  68. default_ioport_writel
  69. };
  70. IOPortWriteFunc *func = ioport_write_table[index][address];
  71. if (!func)
  72. func = default_func[index];
  73. func(ioport_opaque[address], address, data);
  74. }
  75. static uint32_t default_ioport_readb(void *opaque, uint32_t address)
  76. {
  77. LOG_UNUSED_IOPORT("unused inb: port=0x%04"PRIx32"\n", address);
  78. return 0xff;
  79. }
  80. static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
  81. {
  82. LOG_UNUSED_IOPORT("unused outb: port=0x%04"PRIx32" data=0x%02"PRIx32"\n",
  83. address, data);
  84. }
  85. /* default is to make two byte accesses */
  86. static uint32_t default_ioport_readw(void *opaque, uint32_t address)
  87. {
  88. uint32_t data;
  89. data = ioport_read(0, address);
  90. address = (address + 1) & IOPORTS_MASK;
  91. data |= ioport_read(0, address) << 8;
  92. return data;
  93. }
  94. static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
  95. {
  96. ioport_write(0, address, data & 0xff);
  97. address = (address + 1) & IOPORTS_MASK;
  98. ioport_write(0, address, (data >> 8) & 0xff);
  99. }
  100. static uint32_t default_ioport_readl(void *opaque, uint32_t address)
  101. {
  102. LOG_UNUSED_IOPORT("unused inl: port=0x%04"PRIx32"\n", address);
  103. return 0xffffffff;
  104. }
  105. static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
  106. {
  107. LOG_UNUSED_IOPORT("unused outl: port=0x%04"PRIx32" data=0x%02"PRIx32"\n",
  108. address, data);
  109. }
  110. static int ioport_bsize(int size, int *bsize)
  111. {
  112. if (size == 1) {
  113. *bsize = 0;
  114. } else if (size == 2) {
  115. *bsize = 1;
  116. } else if (size == 4) {
  117. *bsize = 2;
  118. } else {
  119. return -1;
  120. }
  121. return 0;
  122. }
  123. /* size is the word size in byte */
  124. int register_ioport_read(pio_addr_t start, int length, int size,
  125. IOPortReadFunc *func, void *opaque)
  126. {
  127. int i, bsize;
  128. if (ioport_bsize(size, &bsize)) {
  129. hw_error("register_ioport_read: invalid size");
  130. return -1;
  131. }
  132. for(i = start; i < start + length; ++i) {
  133. ioport_read_table[bsize][i] = func;
  134. if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
  135. hw_error("register_ioport_read: invalid opaque for address 0x%x",
  136. i);
  137. ioport_opaque[i] = opaque;
  138. }
  139. return 0;
  140. }
  141. /* size is the word size in byte */
  142. int register_ioport_write(pio_addr_t start, int length, int size,
  143. IOPortWriteFunc *func, void *opaque)
  144. {
  145. int i, bsize;
  146. if (ioport_bsize(size, &bsize)) {
  147. hw_error("register_ioport_write: invalid size");
  148. return -1;
  149. }
  150. for(i = start; i < start + length; ++i) {
  151. ioport_write_table[bsize][i] = func;
  152. if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
  153. hw_error("register_ioport_write: invalid opaque for address 0x%x",
  154. i);
  155. ioport_opaque[i] = opaque;
  156. }
  157. return 0;
  158. }
  159. static uint32_t ioport_readb_thunk(void *opaque, uint32_t addr)
  160. {
  161. IORange *ioport = opaque;
  162. uint64_t data;
  163. ioport->ops->read(ioport, addr - ioport->base, 1, &data);
  164. return data;
  165. }
  166. static uint32_t ioport_readw_thunk(void *opaque, uint32_t addr)
  167. {
  168. IORange *ioport = opaque;
  169. uint64_t data;
  170. ioport->ops->read(ioport, addr - ioport->base, 2, &data);
  171. return data;
  172. }
  173. static uint32_t ioport_readl_thunk(void *opaque, uint32_t addr)
  174. {
  175. IORange *ioport = opaque;
  176. uint64_t data;
  177. ioport->ops->read(ioport, addr - ioport->base, 4, &data);
  178. return data;
  179. }
  180. static void ioport_writeb_thunk(void *opaque, uint32_t addr, uint32_t data)
  181. {
  182. IORange *ioport = opaque;
  183. ioport->ops->write(ioport, addr - ioport->base, 1, data);
  184. }
  185. static void ioport_writew_thunk(void *opaque, uint32_t addr, uint32_t data)
  186. {
  187. IORange *ioport = opaque;
  188. ioport->ops->write(ioport, addr - ioport->base, 2, data);
  189. }
  190. static void ioport_writel_thunk(void *opaque, uint32_t addr, uint32_t data)
  191. {
  192. IORange *ioport = opaque;
  193. ioport->ops->write(ioport, addr - ioport->base, 4, data);
  194. }
  195. static void iorange_destructor_thunk(void *opaque)
  196. {
  197. IORange *iorange = opaque;
  198. if (iorange->ops->destructor) {
  199. iorange->ops->destructor(iorange);
  200. }
  201. }
  202. void ioport_register(IORange *ioport)
  203. {
  204. register_ioport_read(ioport->base, ioport->len, 1,
  205. ioport_readb_thunk, ioport);
  206. register_ioport_read(ioport->base, ioport->len, 2,
  207. ioport_readw_thunk, ioport);
  208. register_ioport_read(ioport->base, ioport->len, 4,
  209. ioport_readl_thunk, ioport);
  210. register_ioport_write(ioport->base, ioport->len, 1,
  211. ioport_writeb_thunk, ioport);
  212. register_ioport_write(ioport->base, ioport->len, 2,
  213. ioport_writew_thunk, ioport);
  214. register_ioport_write(ioport->base, ioport->len, 4,
  215. ioport_writel_thunk, ioport);
  216. ioport_destructor_table[ioport->base] = iorange_destructor_thunk;
  217. }
  218. void isa_unassign_ioport(pio_addr_t start, int length)
  219. {
  220. int i;
  221. if (ioport_destructor_table[start]) {
  222. ioport_destructor_table[start](ioport_opaque[start]);
  223. ioport_destructor_table[start] = NULL;
  224. }
  225. for(i = start; i < start + length; i++) {
  226. ioport_read_table[0][i] = NULL;
  227. ioport_read_table[1][i] = NULL;
  228. ioport_read_table[2][i] = NULL;
  229. ioport_write_table[0][i] = NULL;
  230. ioport_write_table[1][i] = NULL;
  231. ioport_write_table[2][i] = NULL;
  232. ioport_opaque[i] = NULL;
  233. }
  234. }
  235. bool isa_is_ioport_assigned(pio_addr_t start)
  236. {
  237. return (ioport_read_table[0][start] || ioport_write_table[0][start] ||
  238. ioport_read_table[1][start] || ioport_write_table[1][start] ||
  239. ioport_read_table[2][start] || ioport_write_table[2][start]);
  240. }
  241. /***********************************************************/
  242. void cpu_outb(pio_addr_t addr, uint8_t val)
  243. {
  244. LOG_IOPORT("outb: %04"FMT_pioaddr" %02"PRIx8"\n", addr, val);
  245. trace_cpu_out(addr, val);
  246. ioport_write(0, addr, val);
  247. }
  248. void cpu_outw(pio_addr_t addr, uint16_t val)
  249. {
  250. LOG_IOPORT("outw: %04"FMT_pioaddr" %04"PRIx16"\n", addr, val);
  251. trace_cpu_out(addr, val);
  252. ioport_write(1, addr, val);
  253. }
  254. void cpu_outl(pio_addr_t addr, uint32_t val)
  255. {
  256. LOG_IOPORT("outl: %04"FMT_pioaddr" %08"PRIx32"\n", addr, val);
  257. trace_cpu_out(addr, val);
  258. ioport_write(2, addr, val);
  259. }
  260. uint8_t cpu_inb(pio_addr_t addr)
  261. {
  262. uint8_t val;
  263. val = ioport_read(0, addr);
  264. trace_cpu_in(addr, val);
  265. LOG_IOPORT("inb : %04"FMT_pioaddr" %02"PRIx8"\n", addr, val);
  266. return val;
  267. }
  268. uint16_t cpu_inw(pio_addr_t addr)
  269. {
  270. uint16_t val;
  271. val = ioport_read(1, addr);
  272. trace_cpu_in(addr, val);
  273. LOG_IOPORT("inw : %04"FMT_pioaddr" %04"PRIx16"\n", addr, val);
  274. return val;
  275. }
  276. uint32_t cpu_inl(pio_addr_t addr)
  277. {
  278. uint32_t val;
  279. val = ioport_read(2, addr);
  280. trace_cpu_in(addr, val);
  281. LOG_IOPORT("inl : %04"FMT_pioaddr" %08"PRIx32"\n", addr, val);
  282. return val;
  283. }
  284. void portio_list_init(PortioList *piolist,
  285. const MemoryRegionPortio *callbacks,
  286. void *opaque, const char *name)
  287. {
  288. unsigned n = 0;
  289. while (callbacks[n].size) {
  290. ++n;
  291. }
  292. piolist->ports = callbacks;
  293. piolist->nr = 0;
  294. piolist->regions = g_new0(MemoryRegion *, n);
  295. piolist->aliases = g_new0(MemoryRegion *, n);
  296. piolist->address_space = NULL;
  297. piolist->opaque = opaque;
  298. piolist->name = name;
  299. }
  300. void portio_list_destroy(PortioList *piolist)
  301. {
  302. g_free(piolist->regions);
  303. g_free(piolist->aliases);
  304. }
  305. static void portio_list_add_1(PortioList *piolist,
  306. const MemoryRegionPortio *pio_init,
  307. unsigned count, unsigned start,
  308. unsigned off_low, unsigned off_high)
  309. {
  310. MemoryRegionPortio *pio;
  311. MemoryRegionOps *ops;
  312. MemoryRegion *region, *alias;
  313. unsigned i;
  314. /* Copy the sub-list and null-terminate it. */
  315. pio = g_new(MemoryRegionPortio, count + 1);
  316. memcpy(pio, pio_init, sizeof(MemoryRegionPortio) * count);
  317. memset(pio + count, 0, sizeof(MemoryRegionPortio));
  318. /* Adjust the offsets to all be zero-based for the region. */
  319. for (i = 0; i < count; ++i) {
  320. pio[i].offset -= off_low;
  321. }
  322. ops = g_new0(MemoryRegionOps, 1);
  323. ops->old_portio = pio;
  324. region = g_new(MemoryRegion, 1);
  325. alias = g_new(MemoryRegion, 1);
  326. /*
  327. * Use an alias so that the callback is called with an absolute address,
  328. * rather than an offset relative to to start + off_low.
  329. */
  330. memory_region_init_io(region, ops, piolist->opaque, piolist->name,
  331. INT64_MAX);
  332. memory_region_init_alias(alias, piolist->name,
  333. region, start + off_low, off_high - off_low);
  334. memory_region_add_subregion(piolist->address_space,
  335. start + off_low, alias);
  336. piolist->regions[piolist->nr] = region;
  337. piolist->aliases[piolist->nr] = alias;
  338. ++piolist->nr;
  339. }
  340. void portio_list_add(PortioList *piolist,
  341. MemoryRegion *address_space,
  342. uint32_t start)
  343. {
  344. const MemoryRegionPortio *pio, *pio_start = piolist->ports;
  345. unsigned int off_low, off_high, off_last, count;
  346. piolist->address_space = address_space;
  347. /* Handle the first entry specially. */
  348. off_last = off_low = pio_start->offset;
  349. off_high = off_low + pio_start->len;
  350. count = 1;
  351. for (pio = pio_start + 1; pio->size != 0; pio++, count++) {
  352. /* All entries must be sorted by offset. */
  353. assert(pio->offset >= off_last);
  354. off_last = pio->offset;
  355. /* If we see a hole, break the region. */
  356. if (off_last > off_high) {
  357. portio_list_add_1(piolist, pio_start, count, start, off_low,
  358. off_high);
  359. /* ... and start collecting anew. */
  360. pio_start = pio;
  361. off_low = off_last;
  362. off_high = off_low + pio->len;
  363. count = 0;
  364. } else if (off_last + pio->len > off_high) {
  365. off_high = off_last + pio->len;
  366. }
  367. }
  368. /* There will always be an open sub-list. */
  369. portio_list_add_1(piolist, pio_start, count, start, off_low, off_high);
  370. }
  371. void portio_list_del(PortioList *piolist)
  372. {
  373. MemoryRegion *mr, *alias;
  374. unsigned i;
  375. for (i = 0; i < piolist->nr; ++i) {
  376. mr = piolist->regions[i];
  377. alias = piolist->aliases[i];
  378. memory_region_del_subregion(piolist->address_space, alias);
  379. memory_region_destroy(alias);
  380. memory_region_destroy(mr);
  381. g_free((MemoryRegionOps *)mr->ops);
  382. g_free(mr);
  383. g_free(alias);
  384. piolist->regions[i] = NULL;
  385. piolist->aliases[i] = NULL;
  386. }
  387. }