interrupt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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/lv1call.h>
  26. #include "platform.h"
  27. #if defined(DEBUG)
  28. #define DBG(fmt...) udbg_printf(fmt)
  29. #else
  30. #define DBG(fmt...) do{if(0)printk(fmt);}while(0)
  31. #endif
  32. /**
  33. * struct ps3_bmp - a per cpu irq status and mask bitmap structure
  34. * @status: 256 bit status bitmap indexed by plug
  35. * @unused_1:
  36. * @mask: 256 bit mask bitmap indexed by plug
  37. * @unused_2:
  38. * @lock:
  39. * @ipi_debug_brk_mask:
  40. *
  41. * The HV mantains per SMT thread mappings of HV outlet to HV plug on
  42. * behalf of the guest. These mappings are implemented as 256 bit guest
  43. * supplied bitmaps indexed by plug number. The addresses of the bitmaps
  44. * are registered with the HV through lv1_configure_irq_state_bitmap().
  45. * The HV requires that the 512 bits of status + mask not cross a page
  46. * boundary. PS3_BMP_MINALIGN is used to define this minimal 64 byte
  47. * alignment.
  48. *
  49. * The HV supports 256 plugs per thread, assigned as {0..255}, for a total
  50. * of 512 plugs supported on a processor. To simplify the logic this
  51. * implementation equates HV plug value to Linux virq value, constrains each
  52. * interrupt to have a system wide unique plug number, and limits the range
  53. * of the plug values to map into the first dword of the bitmaps. This
  54. * gives a usable range of plug values of {NUM_ISA_INTERRUPTS..63}. Note
  55. * that there is no constraint on how many in this set an individual thread
  56. * can acquire.
  57. */
  58. #define PS3_BMP_MINALIGN 64
  59. struct ps3_bmp {
  60. struct {
  61. u64 status;
  62. u64 unused_1[3];
  63. u64 mask;
  64. u64 unused_2[3];
  65. };
  66. u64 ipi_debug_brk_mask;
  67. spinlock_t lock;
  68. };
  69. /**
  70. * struct ps3_private - a per cpu data structure
  71. * @bmp: ps3_bmp structure
  72. * @node: HV logical_ppe_id
  73. * @cpu: HV thread_id
  74. */
  75. struct ps3_private {
  76. struct ps3_bmp bmp __attribute__ ((aligned (PS3_BMP_MINALIGN)));
  77. u64 node;
  78. unsigned int cpu;
  79. };
  80. static DEFINE_PER_CPU(struct ps3_private, ps3_private);
  81. int ps3_alloc_irq(enum ps3_cpu_binding cpu, unsigned long outlet,
  82. unsigned int *virq)
  83. {
  84. int result;
  85. struct ps3_private *pd;
  86. /* This defines the default interrupt distribution policy. */
  87. if (cpu == PS3_BINDING_CPU_ANY)
  88. cpu = 0;
  89. pd = &per_cpu(ps3_private, cpu);
  90. *virq = irq_create_mapping(NULL, outlet);
  91. if (*virq == NO_IRQ) {
  92. pr_debug("%s:%d: irq_create_mapping failed: outlet %lu\n",
  93. __func__, __LINE__, outlet);
  94. result = -ENOMEM;
  95. goto fail_create;
  96. }
  97. /* Binds outlet to cpu + virq. */
  98. result = lv1_connect_irq_plug_ext(pd->node, pd->cpu, *virq, outlet, 0);
  99. if (result) {
  100. pr_info("%s:%d: lv1_connect_irq_plug_ext failed: %s\n",
  101. __func__, __LINE__, ps3_result(result));
  102. result = -EPERM;
  103. goto fail_connect;
  104. }
  105. pr_debug("%s:%d: outlet %lu => cpu %u, virq %u\n", __func__, __LINE__,
  106. outlet, cpu, *virq);
  107. result = set_irq_chip_data(*virq, pd);
  108. if (result) {
  109. pr_debug("%s:%d: set_irq_chip_data failed\n",
  110. __func__, __LINE__);
  111. goto fail_set;
  112. }
  113. return result;
  114. fail_set:
  115. lv1_disconnect_irq_plug_ext(pd->node, pd->cpu, *virq);
  116. fail_connect:
  117. irq_dispose_mapping(*virq);
  118. fail_create:
  119. return result;
  120. }
  121. EXPORT_SYMBOL_GPL(ps3_alloc_irq);
  122. int ps3_free_irq(unsigned int virq)
  123. {
  124. int result;
  125. const struct ps3_private *pd = get_irq_chip_data(virq);
  126. pr_debug("%s:%d: node %lu, cpu %d, virq %u\n", __func__, __LINE__,
  127. pd->node, pd->cpu, virq);
  128. result = lv1_disconnect_irq_plug_ext(pd->node, pd->cpu, virq);
  129. if (result)
  130. pr_info("%s:%d: lv1_disconnect_irq_plug_ext failed: %s\n",
  131. __func__, __LINE__, ps3_result(result));
  132. set_irq_chip_data(virq, NULL);
  133. irq_dispose_mapping(virq);
  134. return result;
  135. }
  136. EXPORT_SYMBOL_GPL(ps3_free_irq);
  137. /**
  138. * ps3_alloc_io_irq - Assign a virq to a system bus device.
  139. * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
  140. * serviced on.
  141. * @interrupt_id: The device interrupt id read from the system repository.
  142. * @virq: The assigned Linux virq.
  143. *
  144. * An io irq represents a non-virtualized device interrupt. interrupt_id
  145. * coresponds to the interrupt number of the interrupt controller.
  146. */
  147. int ps3_alloc_io_irq(enum ps3_cpu_binding cpu, unsigned int interrupt_id,
  148. unsigned int *virq)
  149. {
  150. int result;
  151. unsigned long outlet;
  152. result = lv1_construct_io_irq_outlet(interrupt_id, &outlet);
  153. if (result) {
  154. pr_debug("%s:%d: lv1_construct_io_irq_outlet failed: %s\n",
  155. __func__, __LINE__, ps3_result(result));
  156. return result;
  157. }
  158. result = ps3_alloc_irq(cpu, outlet, virq);
  159. BUG_ON(result);
  160. return result;
  161. }
  162. int ps3_free_io_irq(unsigned int virq)
  163. {
  164. int result;
  165. result = lv1_destruct_io_irq_outlet(virq_to_hw(virq));
  166. if (result)
  167. pr_debug("%s:%d: lv1_destruct_io_irq_outlet failed: %s\n",
  168. __func__, __LINE__, ps3_result(result));
  169. ps3_free_irq(virq);
  170. return result;
  171. }
  172. /**
  173. * ps3_alloc_event_irq - Allocate a virq for use with a system event.
  174. * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
  175. * serviced on.
  176. * @virq: The assigned Linux virq.
  177. *
  178. * The virq can be used with lv1_connect_interrupt_event_receive_port() to
  179. * arrange to receive events, or with ps3_send_event_locally() to signal
  180. * events.
  181. */
  182. int ps3_alloc_event_irq(enum ps3_cpu_binding cpu, unsigned int *virq)
  183. {
  184. int result;
  185. unsigned long outlet;
  186. result = lv1_construct_event_receive_port(&outlet);
  187. if (result) {
  188. pr_debug("%s:%d: lv1_construct_event_receive_port failed: %s\n",
  189. __func__, __LINE__, ps3_result(result));
  190. *virq = NO_IRQ;
  191. return result;
  192. }
  193. result = ps3_alloc_irq(cpu, outlet, virq);
  194. BUG_ON(result);
  195. return result;
  196. }
  197. int ps3_free_event_irq(unsigned int virq)
  198. {
  199. int result;
  200. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  201. result = lv1_destruct_event_receive_port(virq_to_hw(virq));
  202. if (result)
  203. pr_debug("%s:%d: lv1_destruct_event_receive_port failed: %s\n",
  204. __func__, __LINE__, ps3_result(result));
  205. ps3_free_irq(virq);
  206. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  207. return result;
  208. }
  209. int ps3_send_event_locally(unsigned int virq)
  210. {
  211. return lv1_send_event_locally(virq_to_hw(virq));
  212. }
  213. /**
  214. * ps3_connect_event_irq - Assign a virq to a system bus device.
  215. * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
  216. * serviced on.
  217. * @did: The HV device identifier read from the system repository.
  218. * @interrupt_id: The device interrupt id read from the system repository.
  219. * @virq: The assigned Linux virq.
  220. *
  221. * An event irq represents a virtual device interrupt. The interrupt_id
  222. * coresponds to the software interrupt number.
  223. */
  224. int ps3_connect_event_irq(enum ps3_cpu_binding cpu,
  225. const struct ps3_device_id *did, unsigned int interrupt_id,
  226. unsigned int *virq)
  227. {
  228. int result;
  229. result = ps3_alloc_event_irq(cpu, virq);
  230. if (result)
  231. return result;
  232. result = lv1_connect_interrupt_event_receive_port(did->bus_id,
  233. did->dev_id, virq_to_hw(*virq), interrupt_id);
  234. if (result) {
  235. pr_debug("%s:%d: lv1_connect_interrupt_event_receive_port"
  236. " failed: %s\n", __func__, __LINE__,
  237. ps3_result(result));
  238. ps3_free_event_irq(*virq);
  239. *virq = NO_IRQ;
  240. return result;
  241. }
  242. pr_debug("%s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
  243. interrupt_id, *virq);
  244. return 0;
  245. }
  246. int ps3_disconnect_event_irq(const struct ps3_device_id *did,
  247. unsigned int interrupt_id, unsigned int virq)
  248. {
  249. int result;
  250. pr_debug(" -> %s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
  251. interrupt_id, virq);
  252. result = lv1_disconnect_interrupt_event_receive_port(did->bus_id,
  253. did->dev_id, virq_to_hw(virq), interrupt_id);
  254. if (result)
  255. pr_debug("%s:%d: lv1_disconnect_interrupt_event_receive_port"
  256. " failed: %s\n", __func__, __LINE__,
  257. ps3_result(result));
  258. ps3_free_event_irq(virq);
  259. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  260. return result;
  261. }
  262. /**
  263. * ps3_alloc_vuart_irq - Configure the system virtual uart virq.
  264. * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
  265. * serviced on.
  266. * @virt_addr_bmp: The caller supplied virtual uart interrupt bitmap.
  267. * @virq: The assigned Linux virq.
  268. *
  269. * The system supports only a single virtual uart, so multiple calls without
  270. * freeing the interrupt will return a wrong state error.
  271. */
  272. int ps3_alloc_vuart_irq(enum ps3_cpu_binding cpu, void* virt_addr_bmp,
  273. unsigned int *virq)
  274. {
  275. int result;
  276. unsigned long outlet;
  277. u64 lpar_addr;
  278. BUG_ON(!is_kernel_addr((u64)virt_addr_bmp));
  279. lpar_addr = ps3_mm_phys_to_lpar(__pa(virt_addr_bmp));
  280. result = lv1_configure_virtual_uart_irq(lpar_addr, &outlet);
  281. if (result) {
  282. pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
  283. __func__, __LINE__, ps3_result(result));
  284. return result;
  285. }
  286. result = ps3_alloc_irq(cpu, outlet, virq);
  287. BUG_ON(result);
  288. return result;
  289. }
  290. int ps3_free_vuart_irq(unsigned int virq)
  291. {
  292. int result;
  293. result = lv1_deconfigure_virtual_uart_irq();
  294. if (result) {
  295. pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
  296. __func__, __LINE__, ps3_result(result));
  297. return result;
  298. }
  299. ps3_free_irq(virq);
  300. return result;
  301. }
  302. /**
  303. * ps3_alloc_spe_irq - Configure an spe virq.
  304. * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
  305. * serviced on.
  306. * @spe_id: The spe_id returned from lv1_construct_logical_spe().
  307. * @class: The spe interrupt class {0,1,2}.
  308. * @virq: The assigned Linux virq.
  309. *
  310. */
  311. int ps3_alloc_spe_irq(enum ps3_cpu_binding cpu, unsigned long spe_id,
  312. unsigned int class, unsigned int *virq)
  313. {
  314. int result;
  315. unsigned long outlet;
  316. BUG_ON(class > 2);
  317. result = lv1_get_spe_irq_outlet(spe_id, class, &outlet);
  318. if (result) {
  319. pr_debug("%s:%d: lv1_get_spe_irq_outlet failed: %s\n",
  320. __func__, __LINE__, ps3_result(result));
  321. return result;
  322. }
  323. result = ps3_alloc_irq(cpu, outlet, virq);
  324. BUG_ON(result);
  325. return result;
  326. }
  327. int ps3_free_spe_irq(unsigned int virq)
  328. {
  329. ps3_free_irq(virq);
  330. return 0;
  331. }
  332. #define PS3_INVALID_OUTLET ((irq_hw_number_t)-1)
  333. #define PS3_PLUG_MAX 63
  334. #if defined(DEBUG)
  335. static void _dump_64_bmp(const char *header, const u64 *p, unsigned cpu,
  336. const char* func, int line)
  337. {
  338. pr_debug("%s:%d: %s %u {%04lx_%04lx_%04lx_%04lx}\n",
  339. func, line, header, cpu,
  340. *p >> 48, (*p >> 32) & 0xffff, (*p >> 16) & 0xffff,
  341. *p & 0xffff);
  342. }
  343. static void __attribute__ ((unused)) _dump_256_bmp(const char *header,
  344. const u64 *p, unsigned cpu, const char* func, int line)
  345. {
  346. pr_debug("%s:%d: %s %u {%016lx:%016lx:%016lx:%016lx}\n",
  347. func, line, header, cpu, p[0], p[1], p[2], p[3]);
  348. }
  349. #define dump_bmp(_x) _dump_bmp(_x, __func__, __LINE__)
  350. static void _dump_bmp(struct ps3_private* pd, const char* func, int line)
  351. {
  352. unsigned long flags;
  353. spin_lock_irqsave(&pd->bmp.lock, flags);
  354. _dump_64_bmp("stat", &pd->bmp.status, pd->cpu, func, line);
  355. _dump_64_bmp("mask", &pd->bmp.mask, pd->cpu, func, line);
  356. spin_unlock_irqrestore(&pd->bmp.lock, flags);
  357. }
  358. #define dump_mask(_x) _dump_mask(_x, __func__, __LINE__)
  359. static void __attribute__ ((unused)) _dump_mask(struct ps3_private* pd,
  360. const char* func, int line)
  361. {
  362. unsigned long flags;
  363. spin_lock_irqsave(&pd->bmp.lock, flags);
  364. _dump_64_bmp("mask", &pd->bmp.mask, pd->cpu, func, line);
  365. spin_unlock_irqrestore(&pd->bmp.lock, flags);
  366. }
  367. #else
  368. static void dump_bmp(struct ps3_private* pd) {};
  369. #endif /* defined(DEBUG) */
  370. static void ps3_chip_mask(unsigned int virq)
  371. {
  372. struct ps3_private *pd = get_irq_chip_data(virq);
  373. u64 bit = 0x8000000000000000UL >> virq;
  374. u64 *p = &pd->bmp.mask;
  375. u64 old;
  376. unsigned long flags;
  377. pr_debug("%s:%d: cpu %u, virq %d\n", __func__, __LINE__, pd->cpu, virq);
  378. local_irq_save(flags);
  379. asm volatile(
  380. "1: ldarx %0,0,%3\n"
  381. "andc %0,%0,%2\n"
  382. "stdcx. %0,0,%3\n"
  383. "bne- 1b"
  384. : "=&r" (old), "+m" (*p)
  385. : "r" (bit), "r" (p)
  386. : "cc" );
  387. lv1_did_update_interrupt_mask(pd->node, pd->cpu);
  388. local_irq_restore(flags);
  389. }
  390. static void ps3_chip_unmask(unsigned int virq)
  391. {
  392. struct ps3_private *pd = get_irq_chip_data(virq);
  393. u64 bit = 0x8000000000000000UL >> virq;
  394. u64 *p = &pd->bmp.mask;
  395. u64 old;
  396. unsigned long flags;
  397. pr_debug("%s:%d: cpu %u, virq %d\n", __func__, __LINE__, pd->cpu, virq);
  398. local_irq_save(flags);
  399. asm volatile(
  400. "1: ldarx %0,0,%3\n"
  401. "or %0,%0,%2\n"
  402. "stdcx. %0,0,%3\n"
  403. "bne- 1b"
  404. : "=&r" (old), "+m" (*p)
  405. : "r" (bit), "r" (p)
  406. : "cc" );
  407. lv1_did_update_interrupt_mask(pd->node, pd->cpu);
  408. local_irq_restore(flags);
  409. }
  410. static void ps3_chip_eoi(unsigned int virq)
  411. {
  412. const struct ps3_private *pd = get_irq_chip_data(virq);
  413. lv1_end_of_interrupt_ext(pd->node, pd->cpu, virq);
  414. }
  415. static struct irq_chip irq_chip = {
  416. .typename = "ps3",
  417. .mask = ps3_chip_mask,
  418. .unmask = ps3_chip_unmask,
  419. .eoi = ps3_chip_eoi,
  420. };
  421. static void ps3_host_unmap(struct irq_host *h, unsigned int virq)
  422. {
  423. set_irq_chip_data(virq, NULL);
  424. }
  425. static int ps3_host_map(struct irq_host *h, unsigned int virq,
  426. irq_hw_number_t hwirq)
  427. {
  428. pr_debug("%s:%d: hwirq %lu, virq %u\n", __func__, __LINE__, hwirq,
  429. virq);
  430. set_irq_chip_and_handler(virq, &irq_chip, handle_fasteoi_irq);
  431. return 0;
  432. }
  433. static struct irq_host_ops ps3_host_ops = {
  434. .map = ps3_host_map,
  435. .unmap = ps3_host_unmap,
  436. };
  437. void __init ps3_register_ipi_debug_brk(unsigned int cpu, unsigned int virq)
  438. {
  439. struct ps3_private *pd = &per_cpu(ps3_private, cpu);
  440. pd->bmp.ipi_debug_brk_mask = 0x8000000000000000UL >> virq;
  441. pr_debug("%s:%d: cpu %u, virq %u, mask %lxh\n", __func__, __LINE__,
  442. cpu, virq, pd->bmp.ipi_debug_brk_mask);
  443. }
  444. unsigned int ps3_get_irq(void)
  445. {
  446. struct ps3_private *pd = &__get_cpu_var(ps3_private);
  447. u64 x = (pd->bmp.status & pd->bmp.mask);
  448. unsigned int plug;
  449. /* check for ipi break first to stop this cpu ASAP */
  450. if (x & pd->bmp.ipi_debug_brk_mask)
  451. x &= pd->bmp.ipi_debug_brk_mask;
  452. asm volatile("cntlzd %0,%1" : "=r" (plug) : "r" (x));
  453. plug &= 0x3f;
  454. if (unlikely(plug) == NO_IRQ) {
  455. pr_debug("%s:%d: no plug found: cpu %u\n", __func__, __LINE__,
  456. pd->cpu);
  457. dump_bmp(&per_cpu(ps3_private, 0));
  458. dump_bmp(&per_cpu(ps3_private, 1));
  459. return NO_IRQ;
  460. }
  461. #if defined(DEBUG)
  462. if (unlikely(plug < NUM_ISA_INTERRUPTS || plug > PS3_PLUG_MAX)) {
  463. dump_bmp(&per_cpu(ps3_private, 0));
  464. dump_bmp(&per_cpu(ps3_private, 1));
  465. BUG();
  466. }
  467. #endif
  468. return plug;
  469. }
  470. void __init ps3_init_IRQ(void)
  471. {
  472. int result;
  473. unsigned cpu;
  474. struct irq_host *host;
  475. host = irq_alloc_host(IRQ_HOST_MAP_NOMAP, 0, &ps3_host_ops,
  476. PS3_INVALID_OUTLET);
  477. irq_set_default_host(host);
  478. irq_set_virq_count(PS3_PLUG_MAX + 1);
  479. for_each_possible_cpu(cpu) {
  480. struct ps3_private *pd = &per_cpu(ps3_private, cpu);
  481. lv1_get_logical_ppe_id(&pd->node);
  482. pd->cpu = get_hard_smp_processor_id(cpu);
  483. spin_lock_init(&pd->bmp.lock);
  484. pr_debug("%s:%d: node %lu, cpu %d, bmp %lxh\n", __func__,
  485. __LINE__, pd->node, pd->cpu,
  486. ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
  487. result = lv1_configure_irq_state_bitmap(pd->node, pd->cpu,
  488. ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
  489. if (result)
  490. pr_debug("%s:%d: lv1_configure_irq_state_bitmap failed:"
  491. " %s\n", __func__, __LINE__,
  492. ps3_result(result));
  493. }
  494. ppc_md.get_irq = ps3_get_irq;
  495. }