2
0

edu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/units.h"
  26. #include "hw/pci/pci.h"
  27. #include "hw/hw.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 bool within(uint64_t addr, uint64_t start, uint64_t end)
  93. {
  94. return start <= addr && addr < end;
  95. }
  96. static void edu_check_range(uint64_t addr, uint64_t size1, uint64_t start,
  97. uint64_t size2)
  98. {
  99. uint64_t end1 = addr + size1;
  100. uint64_t end2 = start + size2;
  101. if (within(addr, start, end2) &&
  102. end1 > addr && within(end1, start, end2)) {
  103. return;
  104. }
  105. hw_error("EDU: DMA range 0x%016"PRIx64"-0x%016"PRIx64
  106. " out of bounds (0x%016"PRIx64"-0x%016"PRIx64")!",
  107. addr, end1 - 1, start, end2 - 1);
  108. }
  109. static dma_addr_t edu_clamp_addr(const EduState *edu, dma_addr_t addr)
  110. {
  111. dma_addr_t res = addr & edu->dma_mask;
  112. if (addr != res) {
  113. printf("EDU: clamping DMA %#.16"PRIx64" to %#.16"PRIx64"!\n", addr, res);
  114. }
  115. return res;
  116. }
  117. static void edu_dma_timer(void *opaque)
  118. {
  119. EduState *edu = opaque;
  120. bool raise_irq = false;
  121. if (!(edu->dma.cmd & EDU_DMA_RUN)) {
  122. return;
  123. }
  124. if (EDU_DMA_DIR(edu->dma.cmd) == EDU_DMA_FROM_PCI) {
  125. uint64_t dst = edu->dma.dst;
  126. edu_check_range(dst, edu->dma.cnt, DMA_START, DMA_SIZE);
  127. dst -= DMA_START;
  128. pci_dma_read(&edu->pdev, edu_clamp_addr(edu, edu->dma.src),
  129. edu->dma_buf + dst, edu->dma.cnt);
  130. } else {
  131. uint64_t src = edu->dma.src;
  132. edu_check_range(src, edu->dma.cnt, DMA_START, DMA_SIZE);
  133. src -= DMA_START;
  134. pci_dma_write(&edu->pdev, edu_clamp_addr(edu, edu->dma.dst),
  135. edu->dma_buf + src, edu->dma.cnt);
  136. }
  137. edu->dma.cmd &= ~EDU_DMA_RUN;
  138. if (edu->dma.cmd & EDU_DMA_IRQ) {
  139. raise_irq = true;
  140. }
  141. if (raise_irq) {
  142. edu_raise_irq(edu, DMA_IRQ);
  143. }
  144. }
  145. static void dma_rw(EduState *edu, bool write, dma_addr_t *val, dma_addr_t *dma,
  146. bool timer)
  147. {
  148. if (write && (edu->dma.cmd & EDU_DMA_RUN)) {
  149. return;
  150. }
  151. if (write) {
  152. *dma = *val;
  153. } else {
  154. *val = *dma;
  155. }
  156. if (timer) {
  157. timer_mod(&edu->dma_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 100);
  158. }
  159. }
  160. static uint64_t edu_mmio_read(void *opaque, hwaddr addr, unsigned size)
  161. {
  162. EduState *edu = opaque;
  163. uint64_t val = ~0ULL;
  164. if (addr < 0x80 && size != 4) {
  165. return val;
  166. }
  167. if (addr >= 0x80 && size != 4 && size != 8) {
  168. return val;
  169. }
  170. switch (addr) {
  171. case 0x00:
  172. val = 0x010000edu;
  173. break;
  174. case 0x04:
  175. val = edu->addr4;
  176. break;
  177. case 0x08:
  178. qemu_mutex_lock(&edu->thr_mutex);
  179. val = edu->fact;
  180. qemu_mutex_unlock(&edu->thr_mutex);
  181. break;
  182. case 0x20:
  183. val = qatomic_read(&edu->status);
  184. break;
  185. case 0x24:
  186. val = edu->irq_status;
  187. break;
  188. case 0x80:
  189. dma_rw(edu, false, &val, &edu->dma.src, false);
  190. break;
  191. case 0x88:
  192. dma_rw(edu, false, &val, &edu->dma.dst, false);
  193. break;
  194. case 0x90:
  195. dma_rw(edu, false, &val, &edu->dma.cnt, false);
  196. break;
  197. case 0x98:
  198. dma_rw(edu, false, &val, &edu->dma.cmd, false);
  199. break;
  200. }
  201. return val;
  202. }
  203. static void edu_mmio_write(void *opaque, hwaddr addr, uint64_t val,
  204. unsigned size)
  205. {
  206. EduState *edu = opaque;
  207. if (addr < 0x80 && size != 4) {
  208. return;
  209. }
  210. if (addr >= 0x80 && size != 4 && size != 8) {
  211. return;
  212. }
  213. switch (addr) {
  214. case 0x04:
  215. edu->addr4 = ~val;
  216. break;
  217. case 0x08:
  218. if (qatomic_read(&edu->status) & EDU_STATUS_COMPUTING) {
  219. break;
  220. }
  221. /* EDU_STATUS_COMPUTING cannot go 0->1 concurrently, because it is only
  222. * set in this function and it is under the iothread mutex.
  223. */
  224. qemu_mutex_lock(&edu->thr_mutex);
  225. edu->fact = val;
  226. qatomic_or(&edu->status, EDU_STATUS_COMPUTING);
  227. qemu_cond_signal(&edu->thr_cond);
  228. qemu_mutex_unlock(&edu->thr_mutex);
  229. break;
  230. case 0x20:
  231. if (val & EDU_STATUS_IRQFACT) {
  232. qatomic_or(&edu->status, EDU_STATUS_IRQFACT);
  233. /* Order check of the COMPUTING flag after setting IRQFACT. */
  234. smp_mb__after_rmw();
  235. } else {
  236. qatomic_and(&edu->status, ~EDU_STATUS_IRQFACT);
  237. }
  238. break;
  239. case 0x60:
  240. edu_raise_irq(edu, val);
  241. break;
  242. case 0x64:
  243. edu_lower_irq(edu, val);
  244. break;
  245. case 0x80:
  246. dma_rw(edu, true, &val, &edu->dma.src, false);
  247. break;
  248. case 0x88:
  249. dma_rw(edu, true, &val, &edu->dma.dst, false);
  250. break;
  251. case 0x90:
  252. dma_rw(edu, true, &val, &edu->dma.cnt, false);
  253. break;
  254. case 0x98:
  255. if (!(val & EDU_DMA_RUN)) {
  256. break;
  257. }
  258. dma_rw(edu, true, &val, &edu->dma.cmd, true);
  259. break;
  260. }
  261. }
  262. static const MemoryRegionOps edu_mmio_ops = {
  263. .read = edu_mmio_read,
  264. .write = edu_mmio_write,
  265. .endianness = DEVICE_NATIVE_ENDIAN,
  266. .valid = {
  267. .min_access_size = 4,
  268. .max_access_size = 8,
  269. },
  270. .impl = {
  271. .min_access_size = 4,
  272. .max_access_size = 8,
  273. },
  274. };
  275. /*
  276. * We purposely use a thread, so that users are forced to wait for the status
  277. * register.
  278. */
  279. static void *edu_fact_thread(void *opaque)
  280. {
  281. EduState *edu = opaque;
  282. while (1) {
  283. uint32_t val, ret = 1;
  284. qemu_mutex_lock(&edu->thr_mutex);
  285. while ((qatomic_read(&edu->status) & EDU_STATUS_COMPUTING) == 0 &&
  286. !edu->stopping) {
  287. qemu_cond_wait(&edu->thr_cond, &edu->thr_mutex);
  288. }
  289. if (edu->stopping) {
  290. qemu_mutex_unlock(&edu->thr_mutex);
  291. break;
  292. }
  293. val = edu->fact;
  294. qemu_mutex_unlock(&edu->thr_mutex);
  295. while (val > 0) {
  296. ret *= val--;
  297. }
  298. /*
  299. * We should sleep for a random period here, so that students are
  300. * forced to check the status properly.
  301. */
  302. qemu_mutex_lock(&edu->thr_mutex);
  303. edu->fact = ret;
  304. qemu_mutex_unlock(&edu->thr_mutex);
  305. qatomic_and(&edu->status, ~EDU_STATUS_COMPUTING);
  306. /* Clear COMPUTING flag before checking IRQFACT. */
  307. smp_mb__after_rmw();
  308. if (qatomic_read(&edu->status) & EDU_STATUS_IRQFACT) {
  309. qemu_mutex_lock_iothread();
  310. edu_raise_irq(edu, FACT_IRQ);
  311. qemu_mutex_unlock_iothread();
  312. }
  313. }
  314. return NULL;
  315. }
  316. static void pci_edu_realize(PCIDevice *pdev, Error **errp)
  317. {
  318. EduState *edu = EDU(pdev);
  319. uint8_t *pci_conf = pdev->config;
  320. pci_config_set_interrupt_pin(pci_conf, 1);
  321. if (msi_init(pdev, 0, 1, true, false, errp)) {
  322. return;
  323. }
  324. timer_init_ms(&edu->dma_timer, QEMU_CLOCK_VIRTUAL, edu_dma_timer, edu);
  325. qemu_mutex_init(&edu->thr_mutex);
  326. qemu_cond_init(&edu->thr_cond);
  327. qemu_thread_create(&edu->thread, "edu", edu_fact_thread,
  328. edu, QEMU_THREAD_JOINABLE);
  329. memory_region_init_io(&edu->mmio, OBJECT(edu), &edu_mmio_ops, edu,
  330. "edu-mmio", 1 * MiB);
  331. pci_register_bar(pdev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &edu->mmio);
  332. }
  333. static void pci_edu_uninit(PCIDevice *pdev)
  334. {
  335. EduState *edu = EDU(pdev);
  336. qemu_mutex_lock(&edu->thr_mutex);
  337. edu->stopping = true;
  338. qemu_mutex_unlock(&edu->thr_mutex);
  339. qemu_cond_signal(&edu->thr_cond);
  340. qemu_thread_join(&edu->thread);
  341. qemu_cond_destroy(&edu->thr_cond);
  342. qemu_mutex_destroy(&edu->thr_mutex);
  343. timer_del(&edu->dma_timer);
  344. msi_uninit(pdev);
  345. }
  346. static void edu_instance_init(Object *obj)
  347. {
  348. EduState *edu = EDU(obj);
  349. edu->dma_mask = (1UL << 28) - 1;
  350. object_property_add_uint64_ptr(obj, "dma_mask",
  351. &edu->dma_mask, OBJ_PROP_FLAG_READWRITE);
  352. }
  353. static void edu_class_init(ObjectClass *class, void *data)
  354. {
  355. DeviceClass *dc = DEVICE_CLASS(class);
  356. PCIDeviceClass *k = PCI_DEVICE_CLASS(class);
  357. k->realize = pci_edu_realize;
  358. k->exit = pci_edu_uninit;
  359. k->vendor_id = PCI_VENDOR_ID_QEMU;
  360. k->device_id = 0x11e8;
  361. k->revision = 0x10;
  362. k->class_id = PCI_CLASS_OTHERS;
  363. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  364. }
  365. static void pci_edu_register_types(void)
  366. {
  367. static InterfaceInfo interfaces[] = {
  368. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  369. { },
  370. };
  371. static const TypeInfo edu_info = {
  372. .name = TYPE_PCI_EDU_DEVICE,
  373. .parent = TYPE_PCI_DEVICE,
  374. .instance_size = sizeof(EduState),
  375. .instance_init = edu_instance_init,
  376. .class_init = edu_class_init,
  377. .interfaces = interfaces,
  378. };
  379. type_register_static(&edu_info);
  380. }
  381. type_init(pci_edu_register_types)