i8259.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/mm.h>
  31. #include <linux/slab.h>
  32. #include <linux/bitops.h>
  33. #include "irq.h"
  34. #include <linux/kvm_host.h>
  35. #include "trace.h"
  36. #define pr_pic_unimpl(fmt, ...) \
  37. pr_err_ratelimited("pic: " fmt, ## __VA_ARGS__)
  38. static void pic_irq_request(struct kvm *kvm, int level);
  39. static void pic_lock(struct kvm_pic *s)
  40. __acquires(&s->lock)
  41. {
  42. spin_lock(&s->lock);
  43. }
  44. static void pic_unlock(struct kvm_pic *s)
  45. __releases(&s->lock)
  46. {
  47. bool wakeup = s->wakeup_needed;
  48. struct kvm_vcpu *vcpu;
  49. unsigned long i;
  50. s->wakeup_needed = false;
  51. spin_unlock(&s->lock);
  52. if (wakeup) {
  53. kvm_for_each_vcpu(i, vcpu, s->kvm) {
  54. if (kvm_apic_accept_pic_intr(vcpu)) {
  55. kvm_make_request(KVM_REQ_EVENT, vcpu);
  56. kvm_vcpu_kick(vcpu);
  57. return;
  58. }
  59. }
  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(struct kvm_pic *s, int irq, int irq_source_id, int level)
  170. {
  171. int ret, irq_level;
  172. BUG_ON(irq < 0 || irq >= PIC_NUM_PINS);
  173. pic_lock(s);
  174. irq_level = __kvm_irq_line_state(&s->irq_states[irq],
  175. irq_source_id, level);
  176. ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, irq_level);
  177. pic_update_irq(s);
  178. trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
  179. s->pics[irq >> 3].imr, ret == 0);
  180. pic_unlock(s);
  181. return ret;
  182. }
  183. void kvm_pic_clear_all(struct kvm_pic *s, int irq_source_id)
  184. {
  185. int i;
  186. pic_lock(s);
  187. for (i = 0; i < PIC_NUM_PINS; i++)
  188. __clear_bit(irq_source_id, &s->irq_states[i]);
  189. pic_unlock(s);
  190. }
  191. /*
  192. * acknowledge interrupt 'irq'
  193. */
  194. static inline void pic_intack(struct kvm_kpic_state *s, int irq)
  195. {
  196. s->isr |= 1 << irq;
  197. /*
  198. * We don't clear a level sensitive interrupt here
  199. */
  200. if (!(s->elcr & (1 << irq)))
  201. s->irr &= ~(1 << irq);
  202. if (s->auto_eoi) {
  203. if (s->rotate_on_auto_eoi)
  204. s->priority_add = (irq + 1) & 7;
  205. pic_clear_isr(s, irq);
  206. }
  207. }
  208. int kvm_pic_read_irq(struct kvm *kvm)
  209. {
  210. int irq, irq2, intno;
  211. struct kvm_pic *s = kvm->arch.vpic;
  212. s->output = 0;
  213. pic_lock(s);
  214. irq = pic_get_irq(&s->pics[0]);
  215. if (irq >= 0) {
  216. pic_intack(&s->pics[0], irq);
  217. if (irq == 2) {
  218. irq2 = pic_get_irq(&s->pics[1]);
  219. if (irq2 >= 0)
  220. pic_intack(&s->pics[1], irq2);
  221. else
  222. /*
  223. * spurious IRQ on slave controller
  224. */
  225. irq2 = 7;
  226. intno = s->pics[1].irq_base + irq2;
  227. } else
  228. intno = s->pics[0].irq_base + irq;
  229. } else {
  230. /*
  231. * spurious IRQ on host controller
  232. */
  233. irq = 7;
  234. intno = s->pics[0].irq_base + irq;
  235. }
  236. pic_update_irq(s);
  237. pic_unlock(s);
  238. return intno;
  239. }
  240. static void kvm_pic_reset(struct kvm_kpic_state *s)
  241. {
  242. int irq;
  243. unsigned long i;
  244. struct kvm_vcpu *vcpu;
  245. u8 edge_irr = s->irr & ~s->elcr;
  246. bool found = false;
  247. s->last_irr = 0;
  248. s->irr &= s->elcr;
  249. s->imr = 0;
  250. s->priority_add = 0;
  251. s->special_mask = 0;
  252. s->read_reg_select = 0;
  253. if (!s->init4) {
  254. s->special_fully_nested_mode = 0;
  255. s->auto_eoi = 0;
  256. }
  257. s->init_state = 1;
  258. kvm_for_each_vcpu(i, vcpu, s->pics_state->kvm)
  259. if (kvm_apic_accept_pic_intr(vcpu)) {
  260. found = true;
  261. break;
  262. }
  263. if (!found)
  264. return;
  265. for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
  266. if (edge_irr & (1 << irq))
  267. pic_clear_isr(s, irq);
  268. }
  269. static void pic_ioport_write(void *opaque, u32 addr, u32 val)
  270. {
  271. struct kvm_kpic_state *s = opaque;
  272. int priority, cmd, irq;
  273. addr &= 1;
  274. if (addr == 0) {
  275. if (val & 0x10) {
  276. s->init4 = val & 1;
  277. if (val & 0x02)
  278. pr_pic_unimpl("single mode not supported");
  279. if (val & 0x08)
  280. pr_pic_unimpl(
  281. "level sensitive irq not supported");
  282. kvm_pic_reset(s);
  283. } else if (val & 0x08) {
  284. if (val & 0x04)
  285. s->poll = 1;
  286. if (val & 0x02)
  287. s->read_reg_select = val & 1;
  288. if (val & 0x40)
  289. s->special_mask = (val >> 5) & 1;
  290. } else {
  291. cmd = val >> 5;
  292. switch (cmd) {
  293. case 0:
  294. case 4:
  295. s->rotate_on_auto_eoi = cmd >> 2;
  296. break;
  297. case 1: /* end of interrupt */
  298. case 5:
  299. priority = get_priority(s, s->isr);
  300. if (priority != 8) {
  301. irq = (priority + s->priority_add) & 7;
  302. if (cmd == 5)
  303. s->priority_add = (irq + 1) & 7;
  304. pic_clear_isr(s, irq);
  305. pic_update_irq(s->pics_state);
  306. }
  307. break;
  308. case 3:
  309. irq = val & 7;
  310. pic_clear_isr(s, irq);
  311. pic_update_irq(s->pics_state);
  312. break;
  313. case 6:
  314. s->priority_add = (val + 1) & 7;
  315. pic_update_irq(s->pics_state);
  316. break;
  317. case 7:
  318. irq = val & 7;
  319. s->priority_add = (irq + 1) & 7;
  320. pic_clear_isr(s, irq);
  321. pic_update_irq(s->pics_state);
  322. break;
  323. default:
  324. break; /* no operation */
  325. }
  326. }
  327. } else
  328. switch (s->init_state) {
  329. case 0: { /* normal mode */
  330. u8 imr_diff = s->imr ^ val,
  331. off = (s == &s->pics_state->pics[0]) ? 0 : 8;
  332. s->imr = val;
  333. for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
  334. if (imr_diff & (1 << irq))
  335. kvm_fire_mask_notifiers(
  336. s->pics_state->kvm,
  337. SELECT_PIC(irq + off),
  338. irq + off,
  339. !!(s->imr & (1 << irq)));
  340. pic_update_irq(s->pics_state);
  341. break;
  342. }
  343. case 1:
  344. s->irq_base = val & 0xf8;
  345. s->init_state = 2;
  346. break;
  347. case 2:
  348. if (s->init4)
  349. s->init_state = 3;
  350. else
  351. s->init_state = 0;
  352. break;
  353. case 3:
  354. s->special_fully_nested_mode = (val >> 4) & 1;
  355. s->auto_eoi = (val >> 1) & 1;
  356. s->init_state = 0;
  357. break;
  358. }
  359. }
  360. static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
  361. {
  362. int ret;
  363. ret = pic_get_irq(s);
  364. if (ret >= 0) {
  365. if (addr1 >> 7) {
  366. s->pics_state->pics[0].isr &= ~(1 << 2);
  367. s->pics_state->pics[0].irr &= ~(1 << 2);
  368. }
  369. s->irr &= ~(1 << ret);
  370. pic_clear_isr(s, ret);
  371. if (addr1 >> 7 || ret != 2)
  372. pic_update_irq(s->pics_state);
  373. /* Bit 7 is 1, means there's an interrupt */
  374. ret |= 0x80;
  375. } else {
  376. /* Bit 7 is 0, means there's no interrupt */
  377. ret = 0x07;
  378. pic_update_irq(s->pics_state);
  379. }
  380. return ret;
  381. }
  382. static u32 pic_ioport_read(void *opaque, u32 addr)
  383. {
  384. struct kvm_kpic_state *s = opaque;
  385. int ret;
  386. if (s->poll) {
  387. ret = pic_poll_read(s, addr);
  388. s->poll = 0;
  389. } else
  390. if ((addr & 1) == 0)
  391. if (s->read_reg_select)
  392. ret = s->isr;
  393. else
  394. ret = s->irr;
  395. else
  396. ret = s->imr;
  397. return ret;
  398. }
  399. static void elcr_ioport_write(void *opaque, u32 val)
  400. {
  401. struct kvm_kpic_state *s = opaque;
  402. s->elcr = val & s->elcr_mask;
  403. }
  404. static u32 elcr_ioport_read(void *opaque)
  405. {
  406. struct kvm_kpic_state *s = opaque;
  407. return s->elcr;
  408. }
  409. static int picdev_write(struct kvm_pic *s,
  410. gpa_t addr, int len, const void *val)
  411. {
  412. unsigned char data = *(unsigned char *)val;
  413. if (len != 1) {
  414. pr_pic_unimpl("non byte write\n");
  415. return 0;
  416. }
  417. switch (addr) {
  418. case 0x20:
  419. case 0x21:
  420. pic_lock(s);
  421. pic_ioport_write(&s->pics[0], addr, data);
  422. pic_unlock(s);
  423. break;
  424. case 0xa0:
  425. case 0xa1:
  426. pic_lock(s);
  427. pic_ioport_write(&s->pics[1], addr, data);
  428. pic_unlock(s);
  429. break;
  430. case 0x4d0:
  431. case 0x4d1:
  432. pic_lock(s);
  433. elcr_ioport_write(&s->pics[addr & 1], data);
  434. pic_unlock(s);
  435. break;
  436. default:
  437. return -EOPNOTSUPP;
  438. }
  439. return 0;
  440. }
  441. static int picdev_read(struct kvm_pic *s,
  442. gpa_t addr, int len, void *val)
  443. {
  444. unsigned char *data = (unsigned char *)val;
  445. if (len != 1) {
  446. memset(val, 0, len);
  447. pr_pic_unimpl("non byte read\n");
  448. return 0;
  449. }
  450. switch (addr) {
  451. case 0x20:
  452. case 0x21:
  453. case 0xa0:
  454. case 0xa1:
  455. pic_lock(s);
  456. *data = pic_ioport_read(&s->pics[addr >> 7], addr);
  457. pic_unlock(s);
  458. break;
  459. case 0x4d0:
  460. case 0x4d1:
  461. pic_lock(s);
  462. *data = elcr_ioport_read(&s->pics[addr & 1]);
  463. pic_unlock(s);
  464. break;
  465. default:
  466. return -EOPNOTSUPP;
  467. }
  468. return 0;
  469. }
  470. static int picdev_master_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  471. gpa_t addr, int len, const void *val)
  472. {
  473. return picdev_write(container_of(dev, struct kvm_pic, dev_master),
  474. addr, len, val);
  475. }
  476. static int picdev_master_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  477. gpa_t addr, int len, void *val)
  478. {
  479. return picdev_read(container_of(dev, struct kvm_pic, dev_master),
  480. addr, len, val);
  481. }
  482. static int picdev_slave_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  483. gpa_t addr, int len, const void *val)
  484. {
  485. return picdev_write(container_of(dev, struct kvm_pic, dev_slave),
  486. addr, len, val);
  487. }
  488. static int picdev_slave_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  489. gpa_t addr, int len, void *val)
  490. {
  491. return picdev_read(container_of(dev, struct kvm_pic, dev_slave),
  492. addr, len, val);
  493. }
  494. static int picdev_elcr_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  495. gpa_t addr, int len, const void *val)
  496. {
  497. return picdev_write(container_of(dev, struct kvm_pic, dev_elcr),
  498. addr, len, val);
  499. }
  500. static int picdev_elcr_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  501. gpa_t addr, int len, void *val)
  502. {
  503. return picdev_read(container_of(dev, struct kvm_pic, dev_elcr),
  504. addr, len, val);
  505. }
  506. /*
  507. * callback when PIC0 irq status changed
  508. */
  509. static void pic_irq_request(struct kvm *kvm, int level)
  510. {
  511. struct kvm_pic *s = kvm->arch.vpic;
  512. if (!s->output)
  513. s->wakeup_needed = true;
  514. s->output = level;
  515. }
  516. static const struct kvm_io_device_ops picdev_master_ops = {
  517. .read = picdev_master_read,
  518. .write = picdev_master_write,
  519. };
  520. static const struct kvm_io_device_ops picdev_slave_ops = {
  521. .read = picdev_slave_read,
  522. .write = picdev_slave_write,
  523. };
  524. static const struct kvm_io_device_ops picdev_elcr_ops = {
  525. .read = picdev_elcr_read,
  526. .write = picdev_elcr_write,
  527. };
  528. int kvm_pic_init(struct kvm *kvm)
  529. {
  530. struct kvm_pic *s;
  531. int ret;
  532. s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL_ACCOUNT);
  533. if (!s)
  534. return -ENOMEM;
  535. spin_lock_init(&s->lock);
  536. s->kvm = kvm;
  537. s->pics[0].elcr_mask = 0xf8;
  538. s->pics[1].elcr_mask = 0xde;
  539. s->pics[0].pics_state = s;
  540. s->pics[1].pics_state = s;
  541. /*
  542. * Initialize PIO device
  543. */
  544. kvm_iodevice_init(&s->dev_master, &picdev_master_ops);
  545. kvm_iodevice_init(&s->dev_slave, &picdev_slave_ops);
  546. kvm_iodevice_init(&s->dev_elcr, &picdev_elcr_ops);
  547. mutex_lock(&kvm->slots_lock);
  548. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x20, 2,
  549. &s->dev_master);
  550. if (ret < 0)
  551. goto fail_unlock;
  552. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0xa0, 2, &s->dev_slave);
  553. if (ret < 0)
  554. goto fail_unreg_2;
  555. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x4d0, 2, &s->dev_elcr);
  556. if (ret < 0)
  557. goto fail_unreg_1;
  558. mutex_unlock(&kvm->slots_lock);
  559. kvm->arch.vpic = s;
  560. return 0;
  561. fail_unreg_1:
  562. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &s->dev_slave);
  563. fail_unreg_2:
  564. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &s->dev_master);
  565. fail_unlock:
  566. mutex_unlock(&kvm->slots_lock);
  567. kfree(s);
  568. return ret;
  569. }
  570. void kvm_pic_destroy(struct kvm *kvm)
  571. {
  572. struct kvm_pic *vpic = kvm->arch.vpic;
  573. if (!vpic)
  574. return;
  575. mutex_lock(&kvm->slots_lock);
  576. kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_master);
  577. kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_slave);
  578. kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_elcr);
  579. mutex_unlock(&kvm->slots_lock);
  580. kvm->arch.vpic = NULL;
  581. kfree(vpic);
  582. }