i8259.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * 8259 interrupt controller emulation
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. * Copyright (c) 2007 Intel Corporation
  6. * Copyright 2009 Red Hat, Inc. and/or its affiliates.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. * Authors:
  26. * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
  27. * Port from Qemu.
  28. */
  29. #include <linux/mm.h>
  30. #include <linux/slab.h>
  31. #include <linux/bitops.h>
  32. #include "irq.h"
  33. #include <linux/kvm_host.h>
  34. #include "trace.h"
  35. static void pic_irq_request(struct kvm *kvm, int level);
  36. static void pic_lock(struct kvm_pic *s)
  37. __acquires(&s->lock)
  38. {
  39. spin_lock(&s->lock);
  40. }
  41. static void pic_unlock(struct kvm_pic *s)
  42. __releases(&s->lock)
  43. {
  44. bool wakeup = s->wakeup_needed;
  45. struct kvm_vcpu *vcpu, *found = NULL;
  46. int i;
  47. s->wakeup_needed = false;
  48. spin_unlock(&s->lock);
  49. if (wakeup) {
  50. kvm_for_each_vcpu(i, vcpu, s->kvm) {
  51. if (kvm_apic_accept_pic_intr(vcpu)) {
  52. found = vcpu;
  53. break;
  54. }
  55. }
  56. if (!found)
  57. return;
  58. kvm_make_request(KVM_REQ_EVENT, found);
  59. kvm_vcpu_kick(found);
  60. }
  61. }
  62. static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
  63. {
  64. s->isr &= ~(1 << irq);
  65. if (s != &s->pics_state->pics[0])
  66. irq += 8;
  67. /*
  68. * We are dropping lock while calling ack notifiers since ack
  69. * notifier callbacks for assigned devices call into PIC recursively.
  70. * Other interrupt may be delivered to PIC while lock is dropped but
  71. * it should be safe since PIC state is already updated at this stage.
  72. */
  73. pic_unlock(s->pics_state);
  74. kvm_notify_acked_irq(s->pics_state->kvm, SELECT_PIC(irq), irq);
  75. pic_lock(s->pics_state);
  76. }
  77. /*
  78. * set irq level. If an edge is detected, then the IRR is set to 1
  79. */
  80. static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
  81. {
  82. int mask, ret = 1;
  83. mask = 1 << irq;
  84. if (s->elcr & mask) /* level triggered */
  85. if (level) {
  86. ret = !(s->irr & mask);
  87. s->irr |= mask;
  88. s->last_irr |= mask;
  89. } else {
  90. s->irr &= ~mask;
  91. s->last_irr &= ~mask;
  92. }
  93. else /* edge triggered */
  94. if (level) {
  95. if ((s->last_irr & mask) == 0) {
  96. ret = !(s->irr & mask);
  97. s->irr |= mask;
  98. }
  99. s->last_irr |= mask;
  100. } else
  101. s->last_irr &= ~mask;
  102. return (s->imr & mask) ? -1 : ret;
  103. }
  104. /*
  105. * return the highest priority found in mask (highest = smallest
  106. * number). Return 8 if no irq
  107. */
  108. static inline int get_priority(struct kvm_kpic_state *s, int mask)
  109. {
  110. int priority;
  111. if (mask == 0)
  112. return 8;
  113. priority = 0;
  114. while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
  115. priority++;
  116. return priority;
  117. }
  118. /*
  119. * return the pic wanted interrupt. return -1 if none
  120. */
  121. static int pic_get_irq(struct kvm_kpic_state *s)
  122. {
  123. int mask, cur_priority, priority;
  124. mask = s->irr & ~s->imr;
  125. priority = get_priority(s, mask);
  126. if (priority == 8)
  127. return -1;
  128. /*
  129. * compute current priority. If special fully nested mode on the
  130. * master, the IRQ coming from the slave is not taken into account
  131. * for the priority computation.
  132. */
  133. mask = s->isr;
  134. if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
  135. mask &= ~(1 << 2);
  136. cur_priority = get_priority(s, mask);
  137. if (priority < cur_priority)
  138. /*
  139. * higher priority found: an irq should be generated
  140. */
  141. return (priority + s->priority_add) & 7;
  142. else
  143. return -1;
  144. }
  145. /*
  146. * raise irq to CPU if necessary. must be called every time the active
  147. * irq may change
  148. */
  149. static void pic_update_irq(struct kvm_pic *s)
  150. {
  151. int irq2, irq;
  152. irq2 = pic_get_irq(&s->pics[1]);
  153. if (irq2 >= 0) {
  154. /*
  155. * if irq request by slave pic, signal master PIC
  156. */
  157. pic_set_irq1(&s->pics[0], 2, 1);
  158. pic_set_irq1(&s->pics[0], 2, 0);
  159. }
  160. irq = pic_get_irq(&s->pics[0]);
  161. pic_irq_request(s->kvm, irq >= 0);
  162. }
  163. void kvm_pic_update_irq(struct kvm_pic *s)
  164. {
  165. pic_lock(s);
  166. pic_update_irq(s);
  167. pic_unlock(s);
  168. }
  169. int kvm_pic_set_irq(void *opaque, int irq, int level)
  170. {
  171. struct kvm_pic *s = opaque;
  172. int ret = -1;
  173. pic_lock(s);
  174. if (irq >= 0 && irq < PIC_NUM_PINS) {
  175. ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
  176. pic_update_irq(s);
  177. trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
  178. s->pics[irq >> 3].imr, ret == 0);
  179. }
  180. pic_unlock(s);
  181. return ret;
  182. }
  183. /*
  184. * acknowledge interrupt 'irq'
  185. */
  186. static inline void pic_intack(struct kvm_kpic_state *s, int irq)
  187. {
  188. s->isr |= 1 << irq;
  189. /*
  190. * We don't clear a level sensitive interrupt here
  191. */
  192. if (!(s->elcr & (1 << irq)))
  193. s->irr &= ~(1 << irq);
  194. if (s->auto_eoi) {
  195. if (s->rotate_on_auto_eoi)
  196. s->priority_add = (irq + 1) & 7;
  197. pic_clear_isr(s, irq);
  198. }
  199. }
  200. int kvm_pic_read_irq(struct kvm *kvm)
  201. {
  202. int irq, irq2, intno;
  203. struct kvm_pic *s = pic_irqchip(kvm);
  204. pic_lock(s);
  205. irq = pic_get_irq(&s->pics[0]);
  206. if (irq >= 0) {
  207. pic_intack(&s->pics[0], irq);
  208. if (irq == 2) {
  209. irq2 = pic_get_irq(&s->pics[1]);
  210. if (irq2 >= 0)
  211. pic_intack(&s->pics[1], irq2);
  212. else
  213. /*
  214. * spurious IRQ on slave controller
  215. */
  216. irq2 = 7;
  217. intno = s->pics[1].irq_base + irq2;
  218. irq = irq2 + 8;
  219. } else
  220. intno = s->pics[0].irq_base + irq;
  221. } else {
  222. /*
  223. * spurious IRQ on host controller
  224. */
  225. irq = 7;
  226. intno = s->pics[0].irq_base + irq;
  227. }
  228. pic_update_irq(s);
  229. pic_unlock(s);
  230. return intno;
  231. }
  232. void kvm_pic_reset(struct kvm_kpic_state *s)
  233. {
  234. int irq;
  235. struct kvm_vcpu *vcpu0 = s->pics_state->kvm->bsp_vcpu;
  236. u8 irr = s->irr, isr = s->imr;
  237. s->last_irr = 0;
  238. s->irr = 0;
  239. s->imr = 0;
  240. s->isr = 0;
  241. s->priority_add = 0;
  242. s->irq_base = 0;
  243. s->read_reg_select = 0;
  244. s->poll = 0;
  245. s->special_mask = 0;
  246. s->init_state = 0;
  247. s->auto_eoi = 0;
  248. s->rotate_on_auto_eoi = 0;
  249. s->special_fully_nested_mode = 0;
  250. s->init4 = 0;
  251. for (irq = 0; irq < PIC_NUM_PINS/2; irq++) {
  252. if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0))
  253. if (irr & (1 << irq) || isr & (1 << irq)) {
  254. pic_clear_isr(s, irq);
  255. }
  256. }
  257. }
  258. static void pic_ioport_write(void *opaque, u32 addr, u32 val)
  259. {
  260. struct kvm_kpic_state *s = opaque;
  261. int priority, cmd, irq;
  262. addr &= 1;
  263. if (addr == 0) {
  264. if (val & 0x10) {
  265. s->init4 = val & 1;
  266. s->last_irr = 0;
  267. s->imr = 0;
  268. s->priority_add = 0;
  269. s->special_mask = 0;
  270. s->read_reg_select = 0;
  271. if (!s->init4) {
  272. s->special_fully_nested_mode = 0;
  273. s->auto_eoi = 0;
  274. }
  275. s->init_state = 1;
  276. if (val & 0x02)
  277. printk(KERN_ERR "single mode not supported");
  278. if (val & 0x08)
  279. printk(KERN_ERR
  280. "level sensitive irq not supported");
  281. } else if (val & 0x08) {
  282. if (val & 0x04)
  283. s->poll = 1;
  284. if (val & 0x02)
  285. s->read_reg_select = val & 1;
  286. if (val & 0x40)
  287. s->special_mask = (val >> 5) & 1;
  288. } else {
  289. cmd = val >> 5;
  290. switch (cmd) {
  291. case 0:
  292. case 4:
  293. s->rotate_on_auto_eoi = cmd >> 2;
  294. break;
  295. case 1: /* end of interrupt */
  296. case 5:
  297. priority = get_priority(s, s->isr);
  298. if (priority != 8) {
  299. irq = (priority + s->priority_add) & 7;
  300. if (cmd == 5)
  301. s->priority_add = (irq + 1) & 7;
  302. pic_clear_isr(s, irq);
  303. pic_update_irq(s->pics_state);
  304. }
  305. break;
  306. case 3:
  307. irq = val & 7;
  308. pic_clear_isr(s, irq);
  309. pic_update_irq(s->pics_state);
  310. break;
  311. case 6:
  312. s->priority_add = (val + 1) & 7;
  313. pic_update_irq(s->pics_state);
  314. break;
  315. case 7:
  316. irq = val & 7;
  317. s->priority_add = (irq + 1) & 7;
  318. pic_clear_isr(s, irq);
  319. pic_update_irq(s->pics_state);
  320. break;
  321. default:
  322. break; /* no operation */
  323. }
  324. }
  325. } else
  326. switch (s->init_state) {
  327. case 0: { /* normal mode */
  328. u8 imr_diff = s->imr ^ val,
  329. off = (s == &s->pics_state->pics[0]) ? 0 : 8;
  330. s->imr = val;
  331. for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
  332. if (imr_diff & (1 << irq))
  333. kvm_fire_mask_notifiers(
  334. s->pics_state->kvm,
  335. SELECT_PIC(irq + off),
  336. irq + off,
  337. !!(s->imr & (1 << irq)));
  338. pic_update_irq(s->pics_state);
  339. break;
  340. }
  341. case 1:
  342. s->irq_base = val & 0xf8;
  343. s->init_state = 2;
  344. break;
  345. case 2:
  346. if (s->init4)
  347. s->init_state = 3;
  348. else
  349. s->init_state = 0;
  350. break;
  351. case 3:
  352. s->special_fully_nested_mode = (val >> 4) & 1;
  353. s->auto_eoi = (val >> 1) & 1;
  354. s->init_state = 0;
  355. break;
  356. }
  357. }
  358. static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
  359. {
  360. int ret;
  361. ret = pic_get_irq(s);
  362. if (ret >= 0) {
  363. if (addr1 >> 7) {
  364. s->pics_state->pics[0].isr &= ~(1 << 2);
  365. s->pics_state->pics[0].irr &= ~(1 << 2);
  366. }
  367. s->irr &= ~(1 << ret);
  368. pic_clear_isr(s, ret);
  369. if (addr1 >> 7 || ret != 2)
  370. pic_update_irq(s->pics_state);
  371. } else {
  372. ret = 0x07;
  373. pic_update_irq(s->pics_state);
  374. }
  375. return ret;
  376. }
  377. static u32 pic_ioport_read(void *opaque, u32 addr1)
  378. {
  379. struct kvm_kpic_state *s = opaque;
  380. unsigned int addr;
  381. int ret;
  382. addr = addr1;
  383. addr &= 1;
  384. if (s->poll) {
  385. ret = pic_poll_read(s, addr1);
  386. s->poll = 0;
  387. } else
  388. if (addr == 0)
  389. if (s->read_reg_select)
  390. ret = s->isr;
  391. else
  392. ret = s->irr;
  393. else
  394. ret = s->imr;
  395. return ret;
  396. }
  397. static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
  398. {
  399. struct kvm_kpic_state *s = opaque;
  400. s->elcr = val & s->elcr_mask;
  401. }
  402. static u32 elcr_ioport_read(void *opaque, u32 addr1)
  403. {
  404. struct kvm_kpic_state *s = opaque;
  405. return s->elcr;
  406. }
  407. static int picdev_in_range(gpa_t addr)
  408. {
  409. switch (addr) {
  410. case 0x20:
  411. case 0x21:
  412. case 0xa0:
  413. case 0xa1:
  414. case 0x4d0:
  415. case 0x4d1:
  416. return 1;
  417. default:
  418. return 0;
  419. }
  420. }
  421. static inline struct kvm_pic *to_pic(struct kvm_io_device *dev)
  422. {
  423. return container_of(dev, struct kvm_pic, dev);
  424. }
  425. static int picdev_write(struct kvm_io_device *this,
  426. gpa_t addr, int len, const void *val)
  427. {
  428. struct kvm_pic *s = to_pic(this);
  429. unsigned char data = *(unsigned char *)val;
  430. if (!picdev_in_range(addr))
  431. return -EOPNOTSUPP;
  432. if (len != 1) {
  433. if (printk_ratelimit())
  434. printk(KERN_ERR "PIC: non byte write\n");
  435. return 0;
  436. }
  437. pic_lock(s);
  438. switch (addr) {
  439. case 0x20:
  440. case 0x21:
  441. case 0xa0:
  442. case 0xa1:
  443. pic_ioport_write(&s->pics[addr >> 7], addr, data);
  444. break;
  445. case 0x4d0:
  446. case 0x4d1:
  447. elcr_ioport_write(&s->pics[addr & 1], addr, data);
  448. break;
  449. }
  450. pic_unlock(s);
  451. return 0;
  452. }
  453. static int picdev_read(struct kvm_io_device *this,
  454. gpa_t addr, int len, void *val)
  455. {
  456. struct kvm_pic *s = to_pic(this);
  457. unsigned char data = 0;
  458. if (!picdev_in_range(addr))
  459. return -EOPNOTSUPP;
  460. if (len != 1) {
  461. if (printk_ratelimit())
  462. printk(KERN_ERR "PIC: non byte read\n");
  463. return 0;
  464. }
  465. pic_lock(s);
  466. switch (addr) {
  467. case 0x20:
  468. case 0x21:
  469. case 0xa0:
  470. case 0xa1:
  471. data = pic_ioport_read(&s->pics[addr >> 7], addr);
  472. break;
  473. case 0x4d0:
  474. case 0x4d1:
  475. data = elcr_ioport_read(&s->pics[addr & 1], addr);
  476. break;
  477. }
  478. *(unsigned char *)val = data;
  479. pic_unlock(s);
  480. return 0;
  481. }
  482. /*
  483. * callback when PIC0 irq status changed
  484. */
  485. static void pic_irq_request(struct kvm *kvm, int level)
  486. {
  487. struct kvm_pic *s = pic_irqchip(kvm);
  488. if (!s->output)
  489. s->wakeup_needed = true;
  490. s->output = level;
  491. }
  492. static const struct kvm_io_device_ops picdev_ops = {
  493. .read = picdev_read,
  494. .write = picdev_write,
  495. };
  496. struct kvm_pic *kvm_create_pic(struct kvm *kvm)
  497. {
  498. struct kvm_pic *s;
  499. int ret;
  500. s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
  501. if (!s)
  502. return NULL;
  503. spin_lock_init(&s->lock);
  504. s->kvm = kvm;
  505. s->pics[0].elcr_mask = 0xf8;
  506. s->pics[1].elcr_mask = 0xde;
  507. s->pics[0].pics_state = s;
  508. s->pics[1].pics_state = s;
  509. /*
  510. * Initialize PIO device
  511. */
  512. kvm_iodevice_init(&s->dev, &picdev_ops);
  513. mutex_lock(&kvm->slots_lock);
  514. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &s->dev);
  515. mutex_unlock(&kvm->slots_lock);
  516. if (ret < 0) {
  517. kfree(s);
  518. return NULL;
  519. }
  520. return s;
  521. }
  522. void kvm_destroy_pic(struct kvm *kvm)
  523. {
  524. struct kvm_pic *vpic = kvm->arch.vpic;
  525. if (vpic) {
  526. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev);
  527. kvm->arch.vpic = NULL;
  528. kfree(vpic);
  529. }
  530. }