2
0

edu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * QEMU educational PCI device
  3. *
  4. * Copyright (c) 2012-2015 Jiri Slaby
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is 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 THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/log.h"
  26. #include "qemu/units.h"
  27. #include "hw/pci/pci.h"
  28. #include "hw/pci/msi.h"
  29. #include "qemu/timer.h"
  30. #include "qom/object.h"
  31. #include "qemu/main-loop.h" /* iothread mutex */
  32. #include "qemu/module.h"
  33. #include "qapi/visitor.h"
  34. #define TYPE_PCI_EDU_DEVICE "edu"
  35. typedef struct EduState EduState;
  36. DECLARE_INSTANCE_CHECKER(EduState, EDU,
  37. TYPE_PCI_EDU_DEVICE)
  38. #define FACT_IRQ 0x00000001
  39. #define DMA_IRQ 0x00000100
  40. #define DMA_START 0x40000
  41. #define DMA_SIZE 4096
  42. struct EduState {
  43. PCIDevice pdev;
  44. MemoryRegion mmio;
  45. QemuThread thread;
  46. QemuMutex thr_mutex;
  47. QemuCond thr_cond;
  48. bool stopping;
  49. uint32_t addr4;
  50. uint32_t fact;
  51. #define EDU_STATUS_COMPUTING 0x01
  52. #define EDU_STATUS_IRQFACT 0x80
  53. uint32_t status;
  54. uint32_t irq_status;
  55. #define EDU_DMA_RUN 0x1
  56. #define EDU_DMA_DIR(cmd) (((cmd) & 0x2) >> 1)
  57. # define EDU_DMA_FROM_PCI 0
  58. # define EDU_DMA_TO_PCI 1
  59. #define EDU_DMA_IRQ 0x4
  60. struct dma_state {
  61. dma_addr_t src;
  62. dma_addr_t dst;
  63. dma_addr_t cnt;
  64. dma_addr_t cmd;
  65. } dma;
  66. QEMUTimer dma_timer;
  67. char dma_buf[DMA_SIZE];
  68. uint64_t dma_mask;
  69. };
  70. static bool edu_msi_enabled(EduState *edu)
  71. {
  72. return msi_enabled(&edu->pdev);
  73. }
  74. static void edu_raise_irq(EduState *edu, uint32_t val)
  75. {
  76. edu->irq_status |= val;
  77. if (edu->irq_status) {
  78. if (edu_msi_enabled(edu)) {
  79. msi_notify(&edu->pdev, 0);
  80. } else {
  81. pci_set_irq(&edu->pdev, 1);
  82. }
  83. }
  84. }
  85. static void edu_lower_irq(EduState *edu, uint32_t val)
  86. {
  87. edu->irq_status &= ~val;
  88. if (!edu->irq_status && !edu_msi_enabled(edu)) {
  89. pci_set_irq(&edu->pdev, 0);
  90. }
  91. }
  92. static void edu_check_range(uint64_t xfer_start, uint64_t xfer_size,
  93. uint64_t dma_start, uint64_t dma_size)
  94. {
  95. uint64_t xfer_end = xfer_start + xfer_size;
  96. uint64_t dma_end = dma_start + dma_size;
  97. /*
  98. * 1. ensure we aren't overflowing
  99. * 2. ensure that xfer is within dma address range
  100. */
  101. if (dma_end >= dma_start && xfer_end >= xfer_start &&
  102. xfer_start >= dma_start && xfer_end <= dma_end) {
  103. return;
  104. }
  105. qemu_log_mask(LOG_GUEST_ERROR,
  106. "EDU: DMA range 0x%016"PRIx64"-0x%016"PRIx64
  107. " out of bounds (0x%016"PRIx64"-0x%016"PRIx64")!",
  108. xfer_start, xfer_end - 1, dma_start, dma_end - 1);
  109. }
  110. static dma_addr_t edu_clamp_addr(const EduState *edu, dma_addr_t addr)
  111. {
  112. dma_addr_t res = addr & edu->dma_mask;
  113. if (addr != res) {
  114. qemu_log_mask(LOG_GUEST_ERROR,
  115. "EDU: clamping DMA 0x%016"PRIx64" to 0x%016"PRIx64"!",
  116. addr, res);
  117. }
  118. return res;
  119. }
  120. static void edu_dma_timer(void *opaque)
  121. {
  122. EduState *edu = opaque;
  123. bool raise_irq = false;
  124. if (!(edu->dma.cmd & EDU_DMA_RUN)) {
  125. return;
  126. }
  127. if (EDU_DMA_DIR(edu->dma.cmd) == EDU_DMA_FROM_PCI) {
  128. uint64_t dst = edu->dma.dst;
  129. edu_check_range(dst, edu->dma.cnt, DMA_START, DMA_SIZE);
  130. dst -= DMA_START;
  131. pci_dma_read(&edu->pdev, edu_clamp_addr(edu, edu->dma.src),
  132. edu->dma_buf + dst, edu->dma.cnt);
  133. } else {
  134. uint64_t src = edu->dma.src;
  135. edu_check_range(src, edu->dma.cnt, DMA_START, DMA_SIZE);
  136. src -= DMA_START;
  137. pci_dma_write(&edu->pdev, edu_clamp_addr(edu, edu->dma.dst),
  138. edu->dma_buf + src, edu->dma.cnt);
  139. }
  140. edu->dma.cmd &= ~EDU_DMA_RUN;
  141. if (edu->dma.cmd & EDU_DMA_IRQ) {
  142. raise_irq = true;
  143. }
  144. if (raise_irq) {
  145. edu_raise_irq(edu, DMA_IRQ);
  146. }
  147. }
  148. static void dma_rw(EduState *edu, bool write, dma_addr_t *val, dma_addr_t *dma,
  149. bool timer)
  150. {
  151. if (write && (edu->dma.cmd & EDU_DMA_RUN)) {
  152. return;
  153. }
  154. if (write) {
  155. *dma = *val;
  156. } else {
  157. *val = *dma;
  158. }
  159. if (timer) {
  160. timer_mod(&edu->dma_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 100);
  161. }
  162. }
  163. static uint64_t edu_mmio_read(void *opaque, hwaddr addr, unsigned size)
  164. {
  165. EduState *edu = opaque;
  166. uint64_t val = ~0ULL;
  167. if (addr < 0x80 && size != 4) {
  168. return val;
  169. }
  170. if (addr >= 0x80 && size != 4 && size != 8) {
  171. return val;
  172. }
  173. switch (addr) {
  174. case 0x00:
  175. val = 0x010000edu;
  176. break;
  177. case 0x04:
  178. val = edu->addr4;
  179. break;
  180. case 0x08:
  181. qemu_mutex_lock(&edu->thr_mutex);
  182. val = edu->fact;
  183. qemu_mutex_unlock(&edu->thr_mutex);
  184. break;
  185. case 0x20:
  186. val = qatomic_read(&edu->status);
  187. break;
  188. case 0x24:
  189. val = edu->irq_status;
  190. break;
  191. case 0x80:
  192. dma_rw(edu, false, &val, &edu->dma.src, false);
  193. break;
  194. case 0x88:
  195. dma_rw(edu, false, &val, &edu->dma.dst, false);
  196. break;
  197. case 0x90:
  198. dma_rw(edu, false, &val, &edu->dma.cnt, false);
  199. break;
  200. case 0x98:
  201. dma_rw(edu, false, &val, &edu->dma.cmd, false);
  202. break;
  203. }
  204. return val;
  205. }
  206. static void edu_mmio_write(void *opaque, hwaddr addr, uint64_t val,
  207. unsigned size)
  208. {
  209. EduState *edu = opaque;
  210. if (addr < 0x80 && size != 4) {
  211. return;
  212. }
  213. if (addr >= 0x80 && size != 4 && size != 8) {
  214. return;
  215. }
  216. switch (addr) {
  217. case 0x04:
  218. edu->addr4 = ~val;
  219. break;
  220. case 0x08:
  221. if (qatomic_read(&edu->status) & EDU_STATUS_COMPUTING) {
  222. break;
  223. }
  224. /* EDU_STATUS_COMPUTING cannot go 0->1 concurrently, because it is only
  225. * set in this function and it is under the iothread mutex.
  226. */
  227. qemu_mutex_lock(&edu->thr_mutex);
  228. edu->fact = val;
  229. qatomic_or(&edu->status, EDU_STATUS_COMPUTING);
  230. qemu_cond_signal(&edu->thr_cond);
  231. qemu_mutex_unlock(&edu->thr_mutex);
  232. break;
  233. case 0x20:
  234. if (val & EDU_STATUS_IRQFACT) {
  235. qatomic_or(&edu->status, EDU_STATUS_IRQFACT);
  236. /* Order check of the COMPUTING flag after setting IRQFACT. */
  237. smp_mb__after_rmw();
  238. } else {
  239. qatomic_and(&edu->status, ~EDU_STATUS_IRQFACT);
  240. }
  241. break;
  242. case 0x60:
  243. edu_raise_irq(edu, val);
  244. break;
  245. case 0x64:
  246. edu_lower_irq(edu, val);
  247. break;
  248. case 0x80:
  249. dma_rw(edu, true, &val, &edu->dma.src, false);
  250. break;
  251. case 0x88:
  252. dma_rw(edu, true, &val, &edu->dma.dst, false);
  253. break;
  254. case 0x90:
  255. dma_rw(edu, true, &val, &edu->dma.cnt, false);
  256. break;
  257. case 0x98:
  258. if (!(val & EDU_DMA_RUN)) {
  259. break;
  260. }
  261. dma_rw(edu, true, &val, &edu->dma.cmd, true);
  262. break;
  263. }
  264. }
  265. static const MemoryRegionOps edu_mmio_ops = {
  266. .read = edu_mmio_read,
  267. .write = edu_mmio_write,
  268. .endianness = DEVICE_NATIVE_ENDIAN,
  269. .valid = {
  270. .min_access_size = 4,
  271. .max_access_size = 8,
  272. },
  273. .impl = {
  274. .min_access_size = 4,
  275. .max_access_size = 8,
  276. },
  277. };
  278. /*
  279. * We purposely use a thread, so that users are forced to wait for the status
  280. * register.
  281. */
  282. static void *edu_fact_thread(void *opaque)
  283. {
  284. EduState *edu = opaque;
  285. while (1) {
  286. uint32_t val, ret = 1;
  287. qemu_mutex_lock(&edu->thr_mutex);
  288. while ((qatomic_read(&edu->status) & EDU_STATUS_COMPUTING) == 0 &&
  289. !edu->stopping) {
  290. qemu_cond_wait(&edu->thr_cond, &edu->thr_mutex);
  291. }
  292. if (edu->stopping) {
  293. qemu_mutex_unlock(&edu->thr_mutex);
  294. break;
  295. }
  296. val = edu->fact;
  297. qemu_mutex_unlock(&edu->thr_mutex);
  298. while (val > 0) {
  299. ret *= val--;
  300. }
  301. /*
  302. * We should sleep for a random period here, so that students are
  303. * forced to check the status properly.
  304. */
  305. qemu_mutex_lock(&edu->thr_mutex);
  306. edu->fact = ret;
  307. qemu_mutex_unlock(&edu->thr_mutex);
  308. qatomic_and(&edu->status, ~EDU_STATUS_COMPUTING);
  309. /* Clear COMPUTING flag before checking IRQFACT. */
  310. smp_mb__after_rmw();
  311. if (qatomic_read(&edu->status) & EDU_STATUS_IRQFACT) {
  312. bql_lock();
  313. edu_raise_irq(edu, FACT_IRQ);
  314. bql_unlock();
  315. }
  316. }
  317. return NULL;
  318. }
  319. static void pci_edu_realize(PCIDevice *pdev, Error **errp)
  320. {
  321. EduState *edu = EDU(pdev);
  322. uint8_t *pci_conf = pdev->config;
  323. pci_config_set_interrupt_pin(pci_conf, 1);
  324. if (msi_init(pdev, 0, 1, true, false, errp)) {
  325. return;
  326. }
  327. timer_init_ms(&edu->dma_timer, QEMU_CLOCK_VIRTUAL, edu_dma_timer, edu);
  328. qemu_mutex_init(&edu->thr_mutex);
  329. qemu_cond_init(&edu->thr_cond);
  330. qemu_thread_create(&edu->thread, "edu", edu_fact_thread,
  331. edu, QEMU_THREAD_JOINABLE);
  332. memory_region_init_io(&edu->mmio, OBJECT(edu), &edu_mmio_ops, edu,
  333. "edu-mmio", 1 * MiB);
  334. pci_register_bar(pdev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &edu->mmio);
  335. }
  336. static void pci_edu_uninit(PCIDevice *pdev)
  337. {
  338. EduState *edu = EDU(pdev);
  339. qemu_mutex_lock(&edu->thr_mutex);
  340. edu->stopping = true;
  341. qemu_mutex_unlock(&edu->thr_mutex);
  342. qemu_cond_signal(&edu->thr_cond);
  343. qemu_thread_join(&edu->thread);
  344. qemu_cond_destroy(&edu->thr_cond);
  345. qemu_mutex_destroy(&edu->thr_mutex);
  346. timer_del(&edu->dma_timer);
  347. msi_uninit(pdev);
  348. }
  349. static void edu_instance_init(Object *obj)
  350. {
  351. EduState *edu = EDU(obj);
  352. edu->dma_mask = (1UL << 28) - 1;
  353. object_property_add_uint64_ptr(obj, "dma_mask",
  354. &edu->dma_mask, OBJ_PROP_FLAG_READWRITE);
  355. }
  356. static void edu_class_init(ObjectClass *class, void *data)
  357. {
  358. DeviceClass *dc = DEVICE_CLASS(class);
  359. PCIDeviceClass *k = PCI_DEVICE_CLASS(class);
  360. k->realize = pci_edu_realize;
  361. k->exit = pci_edu_uninit;
  362. k->vendor_id = PCI_VENDOR_ID_QEMU;
  363. k->device_id = 0x11e8;
  364. k->revision = 0x10;
  365. k->class_id = PCI_CLASS_OTHERS;
  366. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  367. }
  368. static void pci_edu_register_types(void)
  369. {
  370. static InterfaceInfo interfaces[] = {
  371. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  372. { },
  373. };
  374. static const TypeInfo edu_info = {
  375. .name = TYPE_PCI_EDU_DEVICE,
  376. .parent = TYPE_PCI_DEVICE,
  377. .instance_size = sizeof(EduState),
  378. .instance_init = edu_instance_init,
  379. .class_init = edu_class_init,
  380. .interfaces = interfaces,
  381. };
  382. type_register_static(&edu_info);
  383. }
  384. type_init(pci_edu_register_types)