interrupt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * PS3 interrupt routines.
  3. *
  4. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  5. * Copyright 2006 Sony Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/irq.h>
  23. #include <asm/machdep.h>
  24. #include <asm/udbg.h>
  25. #include <asm/ps3.h>
  26. #include <asm/lv1call.h>
  27. #include "platform.h"
  28. #if defined(DEBUG)
  29. #define DBG(fmt...) udbg_printf(fmt)
  30. #else
  31. #define DBG(fmt...) do{if(0)printk(fmt);}while(0)
  32. #endif
  33. /**
  34. * ps3_alloc_io_irq - Assign a virq to a system bus device.
  35. * interrupt_id: The device interrupt id read from the system repository.
  36. * @virq: The assigned Linux virq.
  37. *
  38. * An io irq represents a non-virtualized device interrupt. interrupt_id
  39. * coresponds to the interrupt number of the interrupt controller.
  40. */
  41. int ps3_alloc_io_irq(unsigned int interrupt_id, unsigned int *virq)
  42. {
  43. int result;
  44. unsigned long outlet;
  45. result = lv1_construct_io_irq_outlet(interrupt_id, &outlet);
  46. if (result) {
  47. pr_debug("%s:%d: lv1_construct_io_irq_outlet failed: %s\n",
  48. __func__, __LINE__, ps3_result(result));
  49. return result;
  50. }
  51. *virq = irq_create_mapping(NULL, outlet);
  52. pr_debug("%s:%d: interrupt_id %u => outlet %lu, virq %u\n",
  53. __func__, __LINE__, interrupt_id, outlet, *virq);
  54. return 0;
  55. }
  56. int ps3_free_io_irq(unsigned int virq)
  57. {
  58. int result;
  59. result = lv1_destruct_io_irq_outlet(virq_to_hw(virq));
  60. if (!result)
  61. pr_debug("%s:%d: lv1_destruct_io_irq_outlet failed: %s\n",
  62. __func__, __LINE__, ps3_result(result));
  63. irq_dispose_mapping(virq);
  64. return result;
  65. }
  66. /**
  67. * ps3_alloc_event_irq - Allocate a virq for use with a system event.
  68. * @virq: The assigned Linux virq.
  69. *
  70. * The virq can be used with lv1_connect_interrupt_event_receive_port() to
  71. * arrange to receive events, or with ps3_send_event_locally() to signal
  72. * events.
  73. */
  74. int ps3_alloc_event_irq(unsigned int *virq)
  75. {
  76. int result;
  77. unsigned long outlet;
  78. result = lv1_construct_event_receive_port(&outlet);
  79. if (result) {
  80. pr_debug("%s:%d: lv1_construct_event_receive_port failed: %s\n",
  81. __func__, __LINE__, ps3_result(result));
  82. *virq = NO_IRQ;
  83. return result;
  84. }
  85. *virq = irq_create_mapping(NULL, outlet);
  86. pr_debug("%s:%d: outlet %lu, virq %u\n", __func__, __LINE__, outlet,
  87. *virq);
  88. return 0;
  89. }
  90. int ps3_free_event_irq(unsigned int virq)
  91. {
  92. int result;
  93. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  94. result = lv1_destruct_event_receive_port(virq_to_hw(virq));
  95. if (result)
  96. pr_debug("%s:%d: lv1_destruct_event_receive_port failed: %s\n",
  97. __func__, __LINE__, ps3_result(result));
  98. irq_dispose_mapping(virq);
  99. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  100. return result;
  101. }
  102. int ps3_send_event_locally(unsigned int virq)
  103. {
  104. return lv1_send_event_locally(virq_to_hw(virq));
  105. }
  106. /**
  107. * ps3_connect_event_irq - Assign a virq to a system bus device.
  108. * @did: The HV device identifier read from the system repository.
  109. * @interrupt_id: The device interrupt id read from the system repository.
  110. * @virq: The assigned Linux virq.
  111. *
  112. * An event irq represents a virtual device interrupt. The interrupt_id
  113. * coresponds to the software interrupt number.
  114. */
  115. int ps3_connect_event_irq(const struct ps3_device_id *did,
  116. unsigned int interrupt_id, unsigned int *virq)
  117. {
  118. int result;
  119. result = ps3_alloc_event_irq(virq);
  120. if (result)
  121. return result;
  122. result = lv1_connect_interrupt_event_receive_port(did->bus_id,
  123. did->dev_id, virq_to_hw(*virq), interrupt_id);
  124. if (result) {
  125. pr_debug("%s:%d: lv1_connect_interrupt_event_receive_port"
  126. " failed: %s\n", __func__, __LINE__,
  127. ps3_result(result));
  128. ps3_free_event_irq(*virq);
  129. *virq = NO_IRQ;
  130. return result;
  131. }
  132. pr_debug("%s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
  133. interrupt_id, *virq);
  134. return 0;
  135. }
  136. int ps3_disconnect_event_irq(const struct ps3_device_id *did,
  137. unsigned int interrupt_id, unsigned int virq)
  138. {
  139. int result;
  140. pr_debug(" -> %s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
  141. interrupt_id, virq);
  142. result = lv1_disconnect_interrupt_event_receive_port(did->bus_id,
  143. did->dev_id, virq_to_hw(virq), interrupt_id);
  144. if (result)
  145. pr_debug("%s:%d: lv1_disconnect_interrupt_event_receive_port"
  146. " failed: %s\n", __func__, __LINE__,
  147. ps3_result(result));
  148. ps3_free_event_irq(virq);
  149. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  150. return result;
  151. }
  152. /**
  153. * ps3_alloc_vuart_irq - Configure the system virtual uart virq.
  154. * @virt_addr_bmp: The caller supplied virtual uart interrupt bitmap.
  155. * @virq: The assigned Linux virq.
  156. *
  157. * The system supports only a single virtual uart, so multiple calls without
  158. * freeing the interrupt will return a wrong state error.
  159. */
  160. int ps3_alloc_vuart_irq(void* virt_addr_bmp, unsigned int *virq)
  161. {
  162. int result;
  163. unsigned long outlet;
  164. unsigned long lpar_addr;
  165. BUG_ON(!is_kernel_addr((unsigned long)virt_addr_bmp));
  166. lpar_addr = ps3_mm_phys_to_lpar(__pa(virt_addr_bmp));
  167. result = lv1_configure_virtual_uart_irq(lpar_addr, &outlet);
  168. if (result) {
  169. pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
  170. __func__, __LINE__, ps3_result(result));
  171. return result;
  172. }
  173. *virq = irq_create_mapping(NULL, outlet);
  174. pr_debug("%s:%d: outlet %lu, virq %u\n", __func__, __LINE__,
  175. outlet, *virq);
  176. return 0;
  177. }
  178. int ps3_free_vuart_irq(unsigned int virq)
  179. {
  180. int result;
  181. result = lv1_deconfigure_virtual_uart_irq();
  182. if (result) {
  183. pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
  184. __func__, __LINE__, ps3_result(result));
  185. return result;
  186. }
  187. irq_dispose_mapping(virq);
  188. return result;
  189. }
  190. /**
  191. * ps3_alloc_spe_irq - Configure an spe virq.
  192. * @spe_id: The spe_id returned from lv1_construct_logical_spe().
  193. * @class: The spe interrupt class {0,1,2}.
  194. * @virq: The assigned Linux virq.
  195. *
  196. */
  197. int ps3_alloc_spe_irq(unsigned long spe_id, unsigned int class,
  198. unsigned int *virq)
  199. {
  200. int result;
  201. unsigned long outlet;
  202. BUG_ON(class > 2);
  203. result = lv1_get_spe_irq_outlet(spe_id, class, &outlet);
  204. if (result) {
  205. pr_debug("%s:%d: lv1_get_spe_irq_outlet failed: %s\n",
  206. __func__, __LINE__, ps3_result(result));
  207. return result;
  208. }
  209. *virq = irq_create_mapping(NULL, outlet);
  210. pr_debug("%s:%d: spe_id %lu, class %u, outlet %lu, virq %u\n",
  211. __func__, __LINE__, spe_id, class, outlet, *virq);
  212. return 0;
  213. }
  214. int ps3_free_spe_irq(unsigned int virq)
  215. {
  216. irq_dispose_mapping(virq);
  217. return 0;
  218. }
  219. #define PS3_INVALID_OUTLET ((irq_hw_number_t)-1)
  220. #define PS3_PLUG_MAX 63
  221. /**
  222. * struct bmp - a per cpu irq status and mask bitmap structure
  223. * @status: 256 bit status bitmap indexed by plug
  224. * @unused_1:
  225. * @mask: 256 bit mask bitmap indexed by plug
  226. * @unused_2:
  227. * @lock:
  228. * @ipi_debug_brk_mask:
  229. *
  230. * The HV mantains per SMT thread mappings of HV outlet to HV plug on
  231. * behalf of the guest. These mappings are implemented as 256 bit guest
  232. * supplied bitmaps indexed by plug number. The address of the bitmaps are
  233. * registered with the HV through lv1_configure_irq_state_bitmap().
  234. *
  235. * The HV supports 256 plugs per thread, assigned as {0..255}, for a total
  236. * of 512 plugs supported on a processor. To simplify the logic this
  237. * implementation equates HV plug value to linux virq value, constrains each
  238. * interrupt to have a system wide unique plug number, and limits the range
  239. * of the plug values to map into the first dword of the bitmaps. This
  240. * gives a usable range of plug values of {NUM_ISA_INTERRUPTS..63}. Note
  241. * that there is no constraint on how many in this set an individual thread
  242. * can aquire.
  243. */
  244. struct bmp {
  245. struct {
  246. unsigned long status;
  247. unsigned long unused_1[3];
  248. unsigned long mask;
  249. unsigned long unused_2[3];
  250. } __attribute__ ((packed));
  251. spinlock_t lock;
  252. unsigned long ipi_debug_brk_mask;
  253. };
  254. /**
  255. * struct private - a per cpu data structure
  256. * @node: HV node id
  257. * @cpu: HV thread id
  258. * @bmp: an HV bmp structure
  259. */
  260. struct private {
  261. unsigned long node;
  262. unsigned int cpu;
  263. struct bmp bmp;
  264. };
  265. #if defined(DEBUG)
  266. static void _dump_64_bmp(const char *header, const unsigned long *p, unsigned cpu,
  267. const char* func, int line)
  268. {
  269. pr_debug("%s:%d: %s %u {%04lx_%04lx_%04lx_%04lx}\n",
  270. func, line, header, cpu,
  271. *p >> 48, (*p >> 32) & 0xffff, (*p >> 16) & 0xffff,
  272. *p & 0xffff);
  273. }
  274. static void __attribute__ ((unused)) _dump_256_bmp(const char *header,
  275. const unsigned long *p, unsigned cpu, const char* func, int line)
  276. {
  277. pr_debug("%s:%d: %s %u {%016lx:%016lx:%016lx:%016lx}\n",
  278. func, line, header, cpu, p[0], p[1], p[2], p[3]);
  279. }
  280. #define dump_bmp(_x) _dump_bmp(_x, __func__, __LINE__)
  281. static void _dump_bmp(struct private* pd, const char* func, int line)
  282. {
  283. unsigned long flags;
  284. spin_lock_irqsave(&pd->bmp.lock, flags);
  285. _dump_64_bmp("stat", &pd->bmp.status, pd->cpu, func, line);
  286. _dump_64_bmp("mask", &pd->bmp.mask, pd->cpu, func, line);
  287. spin_unlock_irqrestore(&pd->bmp.lock, flags);
  288. }
  289. #define dump_mask(_x) _dump_mask(_x, __func__, __LINE__)
  290. static void __attribute__ ((unused)) _dump_mask(struct private* pd,
  291. const char* func, int line)
  292. {
  293. unsigned long flags;
  294. spin_lock_irqsave(&pd->bmp.lock, flags);
  295. _dump_64_bmp("mask", &pd->bmp.mask, pd->cpu, func, line);
  296. spin_unlock_irqrestore(&pd->bmp.lock, flags);
  297. }
  298. #else
  299. static void dump_bmp(struct private* pd) {};
  300. #endif /* defined(DEBUG) */
  301. static void chip_mask(unsigned int virq)
  302. {
  303. unsigned long flags;
  304. struct private *pd = get_irq_chip_data(virq);
  305. pr_debug("%s:%d: cpu %u, virq %d\n", __func__, __LINE__, pd->cpu, virq);
  306. BUG_ON(virq < NUM_ISA_INTERRUPTS);
  307. BUG_ON(virq > PS3_PLUG_MAX);
  308. spin_lock_irqsave(&pd->bmp.lock, flags);
  309. pd->bmp.mask &= ~(0x8000000000000000UL >> virq);
  310. spin_unlock_irqrestore(&pd->bmp.lock, flags);
  311. lv1_did_update_interrupt_mask(pd->node, pd->cpu);
  312. }
  313. static void chip_unmask(unsigned int virq)
  314. {
  315. unsigned long flags;
  316. struct private *pd = get_irq_chip_data(virq);
  317. pr_debug("%s:%d: cpu %u, virq %d\n", __func__, __LINE__, pd->cpu, virq);
  318. BUG_ON(virq < NUM_ISA_INTERRUPTS);
  319. BUG_ON(virq > PS3_PLUG_MAX);
  320. spin_lock_irqsave(&pd->bmp.lock, flags);
  321. pd->bmp.mask |= (0x8000000000000000UL >> virq);
  322. spin_unlock_irqrestore(&pd->bmp.lock, flags);
  323. lv1_did_update_interrupt_mask(pd->node, pd->cpu);
  324. }
  325. static void chip_eoi(unsigned int virq)
  326. {
  327. lv1_end_of_interrupt(virq);
  328. }
  329. static struct irq_chip irq_chip = {
  330. .typename = "ps3",
  331. .mask = chip_mask,
  332. .unmask = chip_unmask,
  333. .eoi = chip_eoi,
  334. };
  335. static void host_unmap(struct irq_host *h, unsigned int virq)
  336. {
  337. int result;
  338. pr_debug("%s:%d: virq %d\n", __func__, __LINE__, virq);
  339. lv1_disconnect_irq_plug(virq);
  340. result = set_irq_chip_data(virq, NULL);
  341. BUG_ON(result);
  342. }
  343. static DEFINE_PER_CPU(struct private, private);
  344. static int host_map(struct irq_host *h, unsigned int virq,
  345. irq_hw_number_t hwirq)
  346. {
  347. int result;
  348. unsigned int cpu;
  349. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  350. pr_debug("%s:%d: hwirq %lu => virq %u\n", __func__, __LINE__, hwirq,
  351. virq);
  352. /* bind this virq to a cpu */
  353. preempt_disable();
  354. cpu = smp_processor_id();
  355. result = lv1_connect_irq_plug(virq, hwirq);
  356. preempt_enable();
  357. if (result) {
  358. pr_info("%s:%d: lv1_connect_irq_plug failed:"
  359. " %s\n", __func__, __LINE__, ps3_result(result));
  360. return -EPERM;
  361. }
  362. result = set_irq_chip_data(virq, &per_cpu(private, cpu));
  363. BUG_ON(result);
  364. set_irq_chip_and_handler(virq, &irq_chip, handle_fasteoi_irq);
  365. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  366. return result;
  367. }
  368. static struct irq_host_ops host_ops = {
  369. .map = host_map,
  370. .unmap = host_unmap,
  371. };
  372. void __init ps3_register_ipi_debug_brk(unsigned int cpu, unsigned int virq)
  373. {
  374. struct private *pd = &per_cpu(private, cpu);
  375. pd->bmp.ipi_debug_brk_mask = 0x8000000000000000UL >> virq;
  376. pr_debug("%s:%d: cpu %u, virq %u, mask %lxh\n", __func__, __LINE__,
  377. cpu, virq, pd->bmp.ipi_debug_brk_mask);
  378. }
  379. static int bmp_get_and_clear_status_bit(struct bmp *m)
  380. {
  381. unsigned long flags;
  382. unsigned int bit;
  383. unsigned long x;
  384. spin_lock_irqsave(&m->lock, flags);
  385. /* check for ipi break first to stop this cpu ASAP */
  386. if (m->status & m->ipi_debug_brk_mask) {
  387. m->status &= ~m->ipi_debug_brk_mask;
  388. spin_unlock_irqrestore(&m->lock, flags);
  389. return __ilog2(m->ipi_debug_brk_mask);
  390. }
  391. x = (m->status & m->mask);
  392. for (bit = NUM_ISA_INTERRUPTS, x <<= bit; x; bit++, x <<= 1)
  393. if (x & 0x8000000000000000UL) {
  394. m->status &= ~(0x8000000000000000UL >> bit);
  395. spin_unlock_irqrestore(&m->lock, flags);
  396. return bit;
  397. }
  398. spin_unlock_irqrestore(&m->lock, flags);
  399. pr_debug("%s:%d: not found\n", __func__, __LINE__);
  400. return -1;
  401. }
  402. unsigned int ps3_get_irq(void)
  403. {
  404. int plug;
  405. struct private *pd = &__get_cpu_var(private);
  406. plug = bmp_get_and_clear_status_bit(&pd->bmp);
  407. if (plug < 1) {
  408. pr_debug("%s:%d: no plug found: cpu %u\n", __func__, __LINE__,
  409. pd->cpu);
  410. dump_bmp(&per_cpu(private, 0));
  411. dump_bmp(&per_cpu(private, 1));
  412. return NO_IRQ;
  413. }
  414. #if defined(DEBUG)
  415. if (plug < NUM_ISA_INTERRUPTS || plug > PS3_PLUG_MAX) {
  416. dump_bmp(&per_cpu(private, 0));
  417. dump_bmp(&per_cpu(private, 1));
  418. BUG();
  419. }
  420. #endif
  421. return plug;
  422. }
  423. void __init ps3_init_IRQ(void)
  424. {
  425. int result;
  426. unsigned long node;
  427. unsigned cpu;
  428. struct irq_host *host;
  429. lv1_get_logical_ppe_id(&node);
  430. host = irq_alloc_host(IRQ_HOST_MAP_NOMAP, 0, &host_ops,
  431. PS3_INVALID_OUTLET);
  432. irq_set_default_host(host);
  433. irq_set_virq_count(PS3_PLUG_MAX + 1);
  434. for_each_possible_cpu(cpu) {
  435. struct private *pd = &per_cpu(private, cpu);
  436. pd->node = node;
  437. pd->cpu = cpu;
  438. spin_lock_init(&pd->bmp.lock);
  439. result = lv1_configure_irq_state_bitmap(node, cpu,
  440. ps3_mm_phys_to_lpar(__pa(&pd->bmp.status)));
  441. if (result)
  442. pr_debug("%s:%d: lv1_configure_irq_state_bitmap failed:"
  443. " %s\n", __func__, __LINE__,
  444. ps3_result(result));
  445. }
  446. ppc_md.get_irq = ps3_get_irq;
  447. }