edu.c 11 KB

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