interrupt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 ps3_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 ps3_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__ ((aligned (64)));
  251. spinlock_t lock;
  252. unsigned long ipi_debug_brk_mask;
  253. };
  254. /**
  255. * struct ps3_private - a per cpu data structure
  256. * @bmp: ps3_bmp structure
  257. * @node: HV logical_ppe_id
  258. * @cpu: HV thread_id
  259. */
  260. struct ps3_private {
  261. struct ps3_bmp bmp;
  262. unsigned long node;
  263. unsigned int cpu;
  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 ps3_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 ps3_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 ps3_private* pd) {};
  300. #endif /* defined(DEBUG) */
  301. static void ps3_chip_mask(unsigned int virq)
  302. {
  303. unsigned long flags;
  304. struct ps3_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. lv1_did_update_interrupt_mask(pd->node, pd->cpu);
  311. spin_unlock_irqrestore(&pd->bmp.lock, flags);
  312. }
  313. static void ps3_chip_unmask(unsigned int virq)
  314. {
  315. unsigned long flags;
  316. struct ps3_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. lv1_did_update_interrupt_mask(pd->node, pd->cpu);
  323. spin_unlock_irqrestore(&pd->bmp.lock, flags);
  324. }
  325. static void ps3_chip_eoi(unsigned int virq)
  326. {
  327. const struct ps3_private *pd = get_irq_chip_data(virq);
  328. lv1_end_of_interrupt_ext(pd->node, pd->cpu, virq);
  329. }
  330. static struct irq_chip irq_chip = {
  331. .typename = "ps3",
  332. .mask = ps3_chip_mask,
  333. .unmask = ps3_chip_unmask,
  334. .eoi = ps3_chip_eoi,
  335. };
  336. static void ps3_host_unmap(struct irq_host *h, unsigned int virq)
  337. {
  338. int result;
  339. const struct ps3_private *pd = get_irq_chip_data(virq);
  340. pr_debug("%s:%d: node %lu, cpu %d, virq %u\n", __func__, __LINE__,
  341. pd->node, pd->cpu, virq);
  342. lv1_disconnect_irq_plug_ext(pd->node, pd->cpu, virq);
  343. result = set_irq_chip_data(virq, NULL);
  344. BUG_ON(result);
  345. }
  346. static DEFINE_PER_CPU(struct ps3_private, ps3_private);
  347. static int ps3_host_map(struct irq_host *h, unsigned int virq,
  348. irq_hw_number_t hwirq)
  349. {
  350. int result;
  351. struct ps3_private *pd = &__get_cpu_var(ps3_private);
  352. pr_debug("%s:%d: node %lu, cpu %d, hwirq %lu => virq %u\n", __func__,
  353. __LINE__, pd->node, pd->cpu, hwirq, virq);
  354. /* Binds this virq to pd->cpu (current cpu) */
  355. result = lv1_connect_irq_plug_ext(pd->node, pd->cpu, virq, hwirq, 0);
  356. if (result) {
  357. pr_info("%s:%d: lv1_connect_irq_plug_ext failed:"
  358. " %s\n", __func__, __LINE__, ps3_result(result));
  359. return -EPERM;
  360. }
  361. result = set_irq_chip_data(virq, pd);
  362. BUG_ON(result);
  363. set_irq_chip_and_handler(virq, &irq_chip, handle_fasteoi_irq);
  364. return result;
  365. }
  366. static struct irq_host_ops ps3_host_ops = {
  367. .map = ps3_host_map,
  368. .unmap = ps3_host_unmap,
  369. };
  370. void __init ps3_register_ipi_debug_brk(unsigned int cpu, unsigned int virq)
  371. {
  372. struct ps3_private *pd = &per_cpu(ps3_private, cpu);
  373. pd->bmp.ipi_debug_brk_mask = 0x8000000000000000UL >> virq;
  374. pr_debug("%s:%d: cpu %u, virq %u, mask %lxh\n", __func__, __LINE__,
  375. cpu, virq, pd->bmp.ipi_debug_brk_mask);
  376. }
  377. static int bmp_get_and_clear_status_bit(struct ps3_bmp *m)
  378. {
  379. unsigned long flags;
  380. unsigned int bit;
  381. unsigned long x;
  382. spin_lock_irqsave(&m->lock, flags);
  383. /* check for ipi break first to stop this cpu ASAP */
  384. if (m->status & m->ipi_debug_brk_mask) {
  385. m->status &= ~m->ipi_debug_brk_mask;
  386. spin_unlock_irqrestore(&m->lock, flags);
  387. return __ilog2(m->ipi_debug_brk_mask);
  388. }
  389. x = (m->status & m->mask);
  390. for (bit = NUM_ISA_INTERRUPTS, x <<= bit; x; bit++, x <<= 1)
  391. if (x & 0x8000000000000000UL) {
  392. m->status &= ~(0x8000000000000000UL >> bit);
  393. spin_unlock_irqrestore(&m->lock, flags);
  394. return bit;
  395. }
  396. spin_unlock_irqrestore(&m->lock, flags);
  397. pr_debug("%s:%d: not found\n", __func__, __LINE__);
  398. return -1;
  399. }
  400. unsigned int ps3_get_irq(void)
  401. {
  402. int plug;
  403. struct ps3_private *pd = &__get_cpu_var(ps3_private);
  404. plug = bmp_get_and_clear_status_bit(&pd->bmp);
  405. if (plug < 1) {
  406. pr_debug("%s:%d: no plug found: cpu %u\n", __func__, __LINE__,
  407. pd->cpu);
  408. dump_bmp(&per_cpu(ps3_private, 0));
  409. dump_bmp(&per_cpu(ps3_private, 1));
  410. return NO_IRQ;
  411. }
  412. #if defined(DEBUG)
  413. if (plug < NUM_ISA_INTERRUPTS || plug > PS3_PLUG_MAX) {
  414. dump_bmp(&per_cpu(ps3_private, 0));
  415. dump_bmp(&per_cpu(ps3_private, 1));
  416. BUG();
  417. }
  418. #endif
  419. return plug;
  420. }
  421. void __init ps3_init_IRQ(void)
  422. {
  423. int result;
  424. unsigned cpu;
  425. struct irq_host *host;
  426. host = irq_alloc_host(IRQ_HOST_MAP_NOMAP, 0, &ps3_host_ops,
  427. PS3_INVALID_OUTLET);
  428. irq_set_default_host(host);
  429. irq_set_virq_count(PS3_PLUG_MAX + 1);
  430. for_each_possible_cpu(cpu) {
  431. struct ps3_private *pd = &per_cpu(ps3_private, cpu);
  432. lv1_get_logical_ppe_id(&pd->node);
  433. pd->cpu = get_hard_smp_processor_id(cpu);
  434. spin_lock_init(&pd->bmp.lock);
  435. pr_debug("%s:%d: node %lu, cpu %d, bmp %lxh\n", __func__,
  436. __LINE__, pd->node, pd->cpu,
  437. ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
  438. result = lv1_configure_irq_state_bitmap(pd->node, pd->cpu,
  439. ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
  440. if (result)
  441. pr_debug("%s:%d: lv1_configure_irq_state_bitmap failed:"
  442. " %s\n", __func__, __LINE__,
  443. ps3_result(result));
  444. }
  445. ppc_md.get_irq = ps3_get_irq;
  446. }