edu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. } else {
  234. qatomic_and(&edu->status, ~EDU_STATUS_IRQFACT);
  235. }
  236. break;
  237. case 0x60:
  238. edu_raise_irq(edu, val);
  239. break;
  240. case 0x64:
  241. edu_lower_irq(edu, val);
  242. break;
  243. case 0x80:
  244. dma_rw(edu, true, &val, &edu->dma.src, false);
  245. break;
  246. case 0x88:
  247. dma_rw(edu, true, &val, &edu->dma.dst, false);
  248. break;
  249. case 0x90:
  250. dma_rw(edu, true, &val, &edu->dma.cnt, false);
  251. break;
  252. case 0x98:
  253. if (!(val & EDU_DMA_RUN)) {
  254. break;
  255. }
  256. dma_rw(edu, true, &val, &edu->dma.cmd, true);
  257. break;
  258. }
  259. }
  260. static const MemoryRegionOps edu_mmio_ops = {
  261. .read = edu_mmio_read,
  262. .write = edu_mmio_write,
  263. .endianness = DEVICE_NATIVE_ENDIAN,
  264. .valid = {
  265. .min_access_size = 4,
  266. .max_access_size = 8,
  267. },
  268. .impl = {
  269. .min_access_size = 4,
  270. .max_access_size = 8,
  271. },
  272. };
  273. /*
  274. * We purposely use a thread, so that users are forced to wait for the status
  275. * register.
  276. */
  277. static void *edu_fact_thread(void *opaque)
  278. {
  279. EduState *edu = opaque;
  280. while (1) {
  281. uint32_t val, ret = 1;
  282. qemu_mutex_lock(&edu->thr_mutex);
  283. while ((qatomic_read(&edu->status) & EDU_STATUS_COMPUTING) == 0 &&
  284. !edu->stopping) {
  285. qemu_cond_wait(&edu->thr_cond, &edu->thr_mutex);
  286. }
  287. if (edu->stopping) {
  288. qemu_mutex_unlock(&edu->thr_mutex);
  289. break;
  290. }
  291. val = edu->fact;
  292. qemu_mutex_unlock(&edu->thr_mutex);
  293. while (val > 0) {
  294. ret *= val--;
  295. }
  296. /*
  297. * We should sleep for a random period here, so that students are
  298. * forced to check the status properly.
  299. */
  300. qemu_mutex_lock(&edu->thr_mutex);
  301. edu->fact = ret;
  302. qemu_mutex_unlock(&edu->thr_mutex);
  303. qatomic_and(&edu->status, ~EDU_STATUS_COMPUTING);
  304. if (qatomic_read(&edu->status) & EDU_STATUS_IRQFACT) {
  305. qemu_mutex_lock_iothread();
  306. edu_raise_irq(edu, FACT_IRQ);
  307. qemu_mutex_unlock_iothread();
  308. }
  309. }
  310. return NULL;
  311. }
  312. static void pci_edu_realize(PCIDevice *pdev, Error **errp)
  313. {
  314. EduState *edu = EDU(pdev);
  315. uint8_t *pci_conf = pdev->config;
  316. pci_config_set_interrupt_pin(pci_conf, 1);
  317. if (msi_init(pdev, 0, 1, true, false, errp)) {
  318. return;
  319. }
  320. timer_init_ms(&edu->dma_timer, QEMU_CLOCK_VIRTUAL, edu_dma_timer, edu);
  321. qemu_mutex_init(&edu->thr_mutex);
  322. qemu_cond_init(&edu->thr_cond);
  323. qemu_thread_create(&edu->thread, "edu", edu_fact_thread,
  324. edu, QEMU_THREAD_JOINABLE);
  325. memory_region_init_io(&edu->mmio, OBJECT(edu), &edu_mmio_ops, edu,
  326. "edu-mmio", 1 * MiB);
  327. pci_register_bar(pdev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &edu->mmio);
  328. }
  329. static void pci_edu_uninit(PCIDevice *pdev)
  330. {
  331. EduState *edu = EDU(pdev);
  332. qemu_mutex_lock(&edu->thr_mutex);
  333. edu->stopping = true;
  334. qemu_mutex_unlock(&edu->thr_mutex);
  335. qemu_cond_signal(&edu->thr_cond);
  336. qemu_thread_join(&edu->thread);
  337. qemu_cond_destroy(&edu->thr_cond);
  338. qemu_mutex_destroy(&edu->thr_mutex);
  339. timer_del(&edu->dma_timer);
  340. msi_uninit(pdev);
  341. }
  342. static void edu_instance_init(Object *obj)
  343. {
  344. EduState *edu = EDU(obj);
  345. edu->dma_mask = (1UL << 28) - 1;
  346. object_property_add_uint64_ptr(obj, "dma_mask",
  347. &edu->dma_mask, OBJ_PROP_FLAG_READWRITE);
  348. }
  349. static void edu_class_init(ObjectClass *class, void *data)
  350. {
  351. DeviceClass *dc = DEVICE_CLASS(class);
  352. PCIDeviceClass *k = PCI_DEVICE_CLASS(class);
  353. k->realize = pci_edu_realize;
  354. k->exit = pci_edu_uninit;
  355. k->vendor_id = PCI_VENDOR_ID_QEMU;
  356. k->device_id = 0x11e8;
  357. k->revision = 0x10;
  358. k->class_id = PCI_CLASS_OTHERS;
  359. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  360. }
  361. static void pci_edu_register_types(void)
  362. {
  363. static InterfaceInfo interfaces[] = {
  364. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  365. { },
  366. };
  367. static const TypeInfo edu_info = {
  368. .name = TYPE_PCI_EDU_DEVICE,
  369. .parent = TYPE_PCI_DEVICE,
  370. .instance_size = sizeof(EduState),
  371. .instance_init = edu_instance_init,
  372. .class_init = edu_class_init,
  373. .interfaces = interfaces,
  374. };
  375. type_register_static(&edu_info);
  376. }
  377. type_init(pci_edu_register_types)