i8259.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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 <[email protected]>
  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. #define pr_pic_unimpl(fmt, ...) \
  36. pr_err_ratelimited("kvm: pic: " fmt, ## __VA_ARGS__)
  37. static void pic_irq_request(struct kvm *kvm, int level);
  38. static void pic_lock(struct kvm_pic *s)
  39. __acquires(&s->lock)
  40. {
  41. spin_lock(&s->lock);
  42. }
  43. static void pic_unlock(struct kvm_pic *s)
  44. __releases(&s->lock)
  45. {
  46. bool wakeup = s->wakeup_needed;
  47. struct kvm_vcpu *vcpu;
  48. unsigned long i;
  49. s->wakeup_needed = false;
  50. spin_unlock(&s->lock);
  51. if (wakeup) {
  52. kvm_for_each_vcpu(i, vcpu, s->kvm) {
  53. if (kvm_apic_accept_pic_intr(vcpu)) {
  54. kvm_make_request(KVM_REQ_EVENT, vcpu);
  55. kvm_vcpu_kick(vcpu);
  56. return;
  57. }
  58. }
  59. }
  60. }
  61. static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
  62. {
  63. s->isr &= ~(1 << irq);
  64. if (s != &s->pics_state->pics[0])
  65. irq += 8;
  66. /*
  67. * We are dropping lock while calling ack notifiers since ack
  68. * notifier callbacks for assigned devices call into PIC recursively.
  69. * Other interrupt may be delivered to PIC while lock is dropped but
  70. * it should be safe since PIC state is already updated at this stage.
  71. */
  72. pic_unlock(s->pics_state);
  73. kvm_notify_acked_irq(s->pics_state->kvm, SELECT_PIC(irq), irq);
  74. pic_lock(s->pics_state);
  75. }
  76. /*
  77. * set irq level. If an edge is detected, then the IRR is set to 1
  78. */
  79. static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
  80. {
  81. int mask, ret = 1;
  82. mask = 1 << irq;
  83. if (s->elcr & mask) /* level triggered */
  84. if (level) {
  85. ret = !(s->irr & mask);
  86. s->irr |= mask;
  87. s->last_irr |= mask;
  88. } else {
  89. s->irr &= ~mask;
  90. s->last_irr &= ~mask;
  91. }
  92. else /* edge triggered */
  93. if (level) {
  94. if ((s->last_irr & mask) == 0) {
  95. ret = !(s->irr & mask);
  96. s->irr |= mask;
  97. }
  98. s->last_irr |= mask;
  99. } else
  100. s->last_irr &= ~mask;
  101. return (s->imr & mask) ? -1 : ret;
  102. }
  103. /*
  104. * return the highest priority found in mask (highest = smallest
  105. * number). Return 8 if no irq
  106. */
  107. static inline int get_priority(struct kvm_kpic_state *s, int mask)
  108. {
  109. int priority;
  110. if (mask == 0)
  111. return 8;
  112. priority = 0;
  113. while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
  114. priority++;
  115. return priority;
  116. }
  117. /*
  118. * return the pic wanted interrupt. return -1 if none
  119. */
  120. static int pic_get_irq(struct kvm_kpic_state *s)
  121. {
  122. int mask, cur_priority, priority;
  123. mask = s->irr & ~s->imr;
  124. priority = get_priority(s, mask);
  125. if (priority == 8)
  126. return -1;
  127. /*
  128. * compute current priority. If special fully nested mode on the
  129. * master, the IRQ coming from the slave is not taken into account
  130. * for the priority computation.
  131. */
  132. mask = s->isr;
  133. if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
  134. mask &= ~(1 << 2);
  135. cur_priority = get_priority(s, mask);
  136. if (priority < cur_priority)
  137. /*
  138. * higher priority found: an irq should be generated
  139. */
  140. return (priority + s->priority_add) & 7;
  141. else
  142. return -1;
  143. }
  144. /*
  145. * raise irq to CPU if necessary. must be called every time the active
  146. * irq may change
  147. */
  148. static void pic_update_irq(struct kvm_pic *s)
  149. {
  150. int irq2, irq;
  151. irq2 = pic_get_irq(&s->pics[1]);
  152. if (irq2 >= 0) {
  153. /*
  154. * if irq request by slave pic, signal master PIC
  155. */
  156. pic_set_irq1(&s->pics[0], 2, 1);
  157. pic_set_irq1(&s->pics[0], 2, 0);
  158. }
  159. irq = pic_get_irq(&s->pics[0]);
  160. pic_irq_request(s->kvm, irq >= 0);
  161. }
  162. void kvm_pic_update_irq(struct kvm_pic *s)
  163. {
  164. pic_lock(s);
  165. pic_update_irq(s);
  166. pic_unlock(s);
  167. }
  168. int kvm_pic_set_irq(struct kvm_pic *s, int irq, int irq_source_id, int level)
  169. {
  170. int ret, irq_level;
  171. BUG_ON(irq < 0 || irq >= PIC_NUM_PINS);
  172. pic_lock(s);
  173. irq_level = __kvm_irq_line_state(&s->irq_states[irq],
  174. irq_source_id, level);
  175. ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, irq_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. pic_unlock(s);
  180. return ret;
  181. }
  182. void kvm_pic_clear_all(struct kvm_pic *s, int irq_source_id)
  183. {
  184. int i;
  185. pic_lock(s);
  186. for (i = 0; i < PIC_NUM_PINS; i++)
  187. __clear_bit(irq_source_id, &s->irq_states[i]);
  188. pic_unlock(s);
  189. }
  190. /*
  191. * acknowledge interrupt 'irq'
  192. */
  193. static inline void pic_intack(struct kvm_kpic_state *s, int irq)
  194. {
  195. s->isr |= 1 << irq;
  196. /*
  197. * We don't clear a level sensitive interrupt here
  198. */
  199. if (!(s->elcr & (1 << irq)))
  200. s->irr &= ~(1 << irq);
  201. if (s->auto_eoi) {
  202. if (s->rotate_on_auto_eoi)
  203. s->priority_add = (irq + 1) & 7;
  204. pic_clear_isr(s, irq);
  205. }
  206. }
  207. int kvm_pic_read_irq(struct kvm *kvm)
  208. {
  209. int irq, irq2, intno;
  210. struct kvm_pic *s = kvm->arch.vpic;
  211. s->output = 0;
  212. pic_lock(s);
  213. irq = pic_get_irq(&s->pics[0]);
  214. if (irq >= 0) {
  215. pic_intack(&s->pics[0], irq);
  216. if (irq == 2) {
  217. irq2 = pic_get_irq(&s->pics[1]);
  218. if (irq2 >= 0)
  219. pic_intack(&s->pics[1], irq2);
  220. else
  221. /*
  222. * spurious IRQ on slave controller
  223. */
  224. irq2 = 7;
  225. intno = s->pics[1].irq_base + irq2;
  226. } else
  227. intno = s->pics[0].irq_base + irq;
  228. } else {
  229. /*
  230. * spurious IRQ on host controller
  231. */
  232. irq = 7;
  233. intno = s->pics[0].irq_base + irq;
  234. }
  235. pic_update_irq(s);
  236. pic_unlock(s);
  237. return intno;
  238. }
  239. static void kvm_pic_reset(struct kvm_kpic_state *s)
  240. {
  241. int irq;
  242. unsigned long i;
  243. struct kvm_vcpu *vcpu;
  244. u8 edge_irr = s->irr & ~s->elcr;
  245. bool found = false;
  246. s->last_irr = 0;
  247. s->irr &= s->elcr;
  248. s->imr = 0;
  249. s->priority_add = 0;
  250. s->special_mask = 0;
  251. s->read_reg_select = 0;
  252. if (!s->init4) {
  253. s->special_fully_nested_mode = 0;
  254. s->auto_eoi = 0;
  255. }
  256. s->init_state = 1;
  257. kvm_for_each_vcpu(i, vcpu, s->pics_state->kvm)
  258. if (kvm_apic_accept_pic_intr(vcpu)) {
  259. found = true;
  260. break;
  261. }
  262. if (!found)
  263. return;
  264. for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
  265. if (edge_irr & (1 << irq))
  266. pic_clear_isr(s, irq);
  267. }
  268. static void pic_ioport_write(void *opaque, u32 addr, u32 val)
  269. {
  270. struct kvm_kpic_state *s = opaque;
  271. int priority, cmd, irq;
  272. addr &= 1;
  273. if (addr == 0) {
  274. if (val & 0x10) {
  275. s->init4 = val & 1;
  276. if (val & 0x02)
  277. pr_pic_unimpl("single mode not supported");
  278. if (val & 0x08)
  279. pr_pic_unimpl(
  280. "level sensitive irq not supported");
  281. kvm_pic_reset(s);
  282. } else if (val & 0x08) {
  283. if (val & 0x04)
  284. s->poll = 1;
  285. if (val & 0x02)
  286. s->read_reg_select = val & 1;
  287. if (val & 0x40)
  288. s->special_mask = (val >> 5) & 1;
  289. } else {
  290. cmd = val >> 5;
  291. switch (cmd) {
  292. case 0:
  293. case 4:
  294. s->rotate_on_auto_eoi = cmd >> 2;
  295. break;
  296. case 1: /* end of interrupt */
  297. case 5:
  298. priority = get_priority(s, s->isr);
  299. if (priority != 8) {
  300. irq = (priority + s->priority_add) & 7;
  301. if (cmd == 5)
  302. s->priority_add = (irq + 1) & 7;
  303. pic_clear_isr(s, irq);
  304. pic_update_irq(s->pics_state);
  305. }
  306. break;
  307. case 3:
  308. irq = val & 7;
  309. pic_clear_isr(s, irq);
  310. pic_update_irq(s->pics_state);
  311. break;
  312. case 6:
  313. s->priority_add = (val + 1) & 7;
  314. pic_update_irq(s->pics_state);
  315. break;
  316. case 7:
  317. irq = val & 7;
  318. s->priority_add = (irq + 1) & 7;
  319. pic_clear_isr(s, irq);
  320. pic_update_irq(s->pics_state);
  321. break;
  322. default:
  323. break; /* no operation */
  324. }
  325. }
  326. } else
  327. switch (s->init_state) {
  328. case 0: { /* normal mode */
  329. u8 imr_diff = s->imr ^ val,
  330. off = (s == &s->pics_state->pics[0]) ? 0 : 8;
  331. s->imr = val;
  332. for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
  333. if (imr_diff & (1 << irq))
  334. kvm_fire_mask_notifiers(
  335. s->pics_state->kvm,
  336. SELECT_PIC(irq + off),
  337. irq + off,
  338. !!(s->imr & (1 << irq)));
  339. pic_update_irq(s->pics_state);
  340. break;
  341. }
  342. case 1:
  343. s->irq_base = val & 0xf8;
  344. s->init_state = 2;
  345. break;
  346. case 2:
  347. if (s->init4)
  348. s->init_state = 3;
  349. else
  350. s->init_state = 0;
  351. break;
  352. case 3:
  353. s->special_fully_nested_mode = (val >> 4) & 1;
  354. s->auto_eoi = (val >> 1) & 1;
  355. s->init_state = 0;
  356. break;
  357. }
  358. }
  359. static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
  360. {
  361. int ret;
  362. ret = pic_get_irq(s);
  363. if (ret >= 0) {
  364. if (addr1 >> 7) {
  365. s->pics_state->pics[0].isr &= ~(1 << 2);
  366. s->pics_state->pics[0].irr &= ~(1 << 2);
  367. }
  368. s->irr &= ~(1 << ret);
  369. pic_clear_isr(s, ret);
  370. if (addr1 >> 7 || ret != 2)
  371. pic_update_irq(s->pics_state);
  372. } else {
  373. ret = 0x07;
  374. pic_update_irq(s->pics_state);
  375. }
  376. return ret;
  377. }
  378. static u32 pic_ioport_read(void *opaque, u32 addr)
  379. {
  380. struct kvm_kpic_state *s = opaque;
  381. int ret;
  382. if (s->poll) {
  383. ret = pic_poll_read(s, addr);
  384. s->poll = 0;
  385. } else
  386. if ((addr & 1) == 0)
  387. if (s->read_reg_select)
  388. ret = s->isr;
  389. else
  390. ret = s->irr;
  391. else
  392. ret = s->imr;
  393. return ret;
  394. }
  395. static void elcr_ioport_write(void *opaque, u32 val)
  396. {
  397. struct kvm_kpic_state *s = opaque;
  398. s->elcr = val & s->elcr_mask;
  399. }
  400. static u32 elcr_ioport_read(void *opaque)
  401. {
  402. struct kvm_kpic_state *s = opaque;
  403. return s->elcr;
  404. }
  405. static int picdev_write(struct kvm_pic *s,
  406. gpa_t addr, int len, const void *val)
  407. {
  408. unsigned char data = *(unsigned char *)val;
  409. if (len != 1) {
  410. pr_pic_unimpl("non byte write\n");
  411. return 0;
  412. }
  413. switch (addr) {
  414. case 0x20:
  415. case 0x21:
  416. pic_lock(s);
  417. pic_ioport_write(&s->pics[0], addr, data);
  418. pic_unlock(s);
  419. break;
  420. case 0xa0:
  421. case 0xa1:
  422. pic_lock(s);
  423. pic_ioport_write(&s->pics[1], addr, data);
  424. pic_unlock(s);
  425. break;
  426. case 0x4d0:
  427. case 0x4d1:
  428. pic_lock(s);
  429. elcr_ioport_write(&s->pics[addr & 1], data);
  430. pic_unlock(s);
  431. break;
  432. default:
  433. return -EOPNOTSUPP;
  434. }
  435. return 0;
  436. }
  437. static int picdev_read(struct kvm_pic *s,
  438. gpa_t addr, int len, void *val)
  439. {
  440. unsigned char *data = (unsigned char *)val;
  441. if (len != 1) {
  442. memset(val, 0, len);
  443. pr_pic_unimpl("non byte read\n");
  444. return 0;
  445. }
  446. switch (addr) {
  447. case 0x20:
  448. case 0x21:
  449. case 0xa0:
  450. case 0xa1:
  451. pic_lock(s);
  452. *data = pic_ioport_read(&s->pics[addr >> 7], addr);
  453. pic_unlock(s);
  454. break;
  455. case 0x4d0:
  456. case 0x4d1:
  457. pic_lock(s);
  458. *data = elcr_ioport_read(&s->pics[addr & 1]);
  459. pic_unlock(s);
  460. break;
  461. default:
  462. return -EOPNOTSUPP;
  463. }
  464. return 0;
  465. }
  466. static int picdev_master_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  467. gpa_t addr, int len, const void *val)
  468. {
  469. return picdev_write(container_of(dev, struct kvm_pic, dev_master),
  470. addr, len, val);
  471. }
  472. static int picdev_master_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  473. gpa_t addr, int len, void *val)
  474. {
  475. return picdev_read(container_of(dev, struct kvm_pic, dev_master),
  476. addr, len, val);
  477. }
  478. static int picdev_slave_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  479. gpa_t addr, int len, const void *val)
  480. {
  481. return picdev_write(container_of(dev, struct kvm_pic, dev_slave),
  482. addr, len, val);
  483. }
  484. static int picdev_slave_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  485. gpa_t addr, int len, void *val)
  486. {
  487. return picdev_read(container_of(dev, struct kvm_pic, dev_slave),
  488. addr, len, val);
  489. }
  490. static int picdev_elcr_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  491. gpa_t addr, int len, const void *val)
  492. {
  493. return picdev_write(container_of(dev, struct kvm_pic, dev_elcr),
  494. addr, len, val);
  495. }
  496. static int picdev_elcr_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  497. gpa_t addr, int len, void *val)
  498. {
  499. return picdev_read(container_of(dev, struct kvm_pic, dev_elcr),
  500. addr, len, val);
  501. }
  502. /*
  503. * callback when PIC0 irq status changed
  504. */
  505. static void pic_irq_request(struct kvm *kvm, int level)
  506. {
  507. struct kvm_pic *s = kvm->arch.vpic;
  508. if (!s->output)
  509. s->wakeup_needed = true;
  510. s->output = level;
  511. }
  512. static const struct kvm_io_device_ops picdev_master_ops = {
  513. .read = picdev_master_read,
  514. .write = picdev_master_write,
  515. };
  516. static const struct kvm_io_device_ops picdev_slave_ops = {
  517. .read = picdev_slave_read,
  518. .write = picdev_slave_write,
  519. };
  520. static const struct kvm_io_device_ops picdev_elcr_ops = {
  521. .read = picdev_elcr_read,
  522. .write = picdev_elcr_write,
  523. };
  524. int kvm_pic_init(struct kvm *kvm)
  525. {
  526. struct kvm_pic *s;
  527. int ret;
  528. s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL_ACCOUNT);
  529. if (!s)
  530. return -ENOMEM;
  531. spin_lock_init(&s->lock);
  532. s->kvm = kvm;
  533. s->pics[0].elcr_mask = 0xf8;
  534. s->pics[1].elcr_mask = 0xde;
  535. s->pics[0].pics_state = s;
  536. s->pics[1].pics_state = s;
  537. /*
  538. * Initialize PIO device
  539. */
  540. kvm_iodevice_init(&s->dev_master, &picdev_master_ops);
  541. kvm_iodevice_init(&s->dev_slave, &picdev_slave_ops);
  542. kvm_iodevice_init(&s->dev_elcr, &picdev_elcr_ops);
  543. mutex_lock(&kvm->slots_lock);
  544. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x20, 2,
  545. &s->dev_master);
  546. if (ret < 0)
  547. goto fail_unlock;
  548. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0xa0, 2, &s->dev_slave);
  549. if (ret < 0)
  550. goto fail_unreg_2;
  551. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x4d0, 2, &s->dev_elcr);
  552. if (ret < 0)
  553. goto fail_unreg_1;
  554. mutex_unlock(&kvm->slots_lock);
  555. kvm->arch.vpic = s;
  556. return 0;
  557. fail_unreg_1:
  558. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &s->dev_slave);
  559. fail_unreg_2:
  560. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &s->dev_master);
  561. fail_unlock:
  562. mutex_unlock(&kvm->slots_lock);
  563. kfree(s);
  564. return ret;
  565. }
  566. void kvm_pic_destroy(struct kvm *kvm)
  567. {
  568. struct kvm_pic *vpic = kvm->arch.vpic;
  569. if (!vpic)
  570. return;
  571. mutex_lock(&kvm->slots_lock);
  572. kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_master);
  573. kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_slave);
  574. kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_elcr);
  575. mutex_unlock(&kvm->slots_lock);
  576. kvm->arch.vpic = NULL;
  577. kfree(vpic);
  578. }