interrupt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. EXPORT_SYMBOL_GPL(ps3_alloc_io_irq);
  163. int ps3_free_io_irq(unsigned int virq)
  164. {
  165. int result;
  166. result = lv1_destruct_io_irq_outlet(virq_to_hw(virq));
  167. if (result)
  168. pr_debug("%s:%d: lv1_destruct_io_irq_outlet failed: %s\n",
  169. __func__, __LINE__, ps3_result(result));
  170. ps3_free_irq(virq);
  171. return result;
  172. }
  173. EXPORT_SYMBOL_GPL(ps3_free_io_irq);
  174. /**
  175. * ps3_alloc_event_irq - Allocate a virq for use with a system event.
  176. * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
  177. * serviced on.
  178. * @virq: The assigned Linux virq.
  179. *
  180. * The virq can be used with lv1_connect_interrupt_event_receive_port() to
  181. * arrange to receive events, or with ps3_send_event_locally() to signal
  182. * events.
  183. */
  184. int ps3_alloc_event_irq(enum ps3_cpu_binding cpu, unsigned int *virq)
  185. {
  186. int result;
  187. unsigned long outlet;
  188. result = lv1_construct_event_receive_port(&outlet);
  189. if (result) {
  190. pr_debug("%s:%d: lv1_construct_event_receive_port failed: %s\n",
  191. __func__, __LINE__, ps3_result(result));
  192. *virq = NO_IRQ;
  193. return result;
  194. }
  195. result = ps3_alloc_irq(cpu, outlet, virq);
  196. BUG_ON(result);
  197. return result;
  198. }
  199. int ps3_free_event_irq(unsigned int virq)
  200. {
  201. int result;
  202. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  203. result = lv1_destruct_event_receive_port(virq_to_hw(virq));
  204. if (result)
  205. pr_debug("%s:%d: lv1_destruct_event_receive_port failed: %s\n",
  206. __func__, __LINE__, ps3_result(result));
  207. ps3_free_irq(virq);
  208. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  209. return result;
  210. }
  211. int ps3_send_event_locally(unsigned int virq)
  212. {
  213. return lv1_send_event_locally(virq_to_hw(virq));
  214. }
  215. /**
  216. * ps3_connect_event_irq - Assign a virq to a system bus device.
  217. * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
  218. * serviced on.
  219. * @did: The HV device identifier read from the system repository.
  220. * @interrupt_id: The device interrupt id read from the system repository.
  221. * @virq: The assigned Linux virq.
  222. *
  223. * An event irq represents a virtual device interrupt. The interrupt_id
  224. * coresponds to the software interrupt number.
  225. */
  226. int ps3_connect_event_irq(enum ps3_cpu_binding cpu,
  227. const struct ps3_device_id *did, unsigned int interrupt_id,
  228. unsigned int *virq)
  229. {
  230. int result;
  231. result = ps3_alloc_event_irq(cpu, virq);
  232. if (result)
  233. return result;
  234. result = lv1_connect_interrupt_event_receive_port(did->bus_id,
  235. did->dev_id, virq_to_hw(*virq), interrupt_id);
  236. if (result) {
  237. pr_debug("%s:%d: lv1_connect_interrupt_event_receive_port"
  238. " failed: %s\n", __func__, __LINE__,
  239. ps3_result(result));
  240. ps3_free_event_irq(*virq);
  241. *virq = NO_IRQ;
  242. return result;
  243. }
  244. pr_debug("%s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
  245. interrupt_id, *virq);
  246. return 0;
  247. }
  248. int ps3_disconnect_event_irq(const struct ps3_device_id *did,
  249. unsigned int interrupt_id, unsigned int virq)
  250. {
  251. int result;
  252. pr_debug(" -> %s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
  253. interrupt_id, virq);
  254. result = lv1_disconnect_interrupt_event_receive_port(did->bus_id,
  255. did->dev_id, virq_to_hw(virq), interrupt_id);
  256. if (result)
  257. pr_debug("%s:%d: lv1_disconnect_interrupt_event_receive_port"
  258. " failed: %s\n", __func__, __LINE__,
  259. ps3_result(result));
  260. ps3_free_event_irq(virq);
  261. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  262. return result;
  263. }
  264. /**
  265. * ps3_alloc_vuart_irq - Configure the system virtual uart virq.
  266. * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
  267. * serviced on.
  268. * @virt_addr_bmp: The caller supplied virtual uart interrupt bitmap.
  269. * @virq: The assigned Linux virq.
  270. *
  271. * The system supports only a single virtual uart, so multiple calls without
  272. * freeing the interrupt will return a wrong state error.
  273. */
  274. int ps3_alloc_vuart_irq(enum ps3_cpu_binding cpu, void* virt_addr_bmp,
  275. unsigned int *virq)
  276. {
  277. int result;
  278. unsigned long outlet;
  279. u64 lpar_addr;
  280. BUG_ON(!is_kernel_addr((u64)virt_addr_bmp));
  281. lpar_addr = ps3_mm_phys_to_lpar(__pa(virt_addr_bmp));
  282. result = lv1_configure_virtual_uart_irq(lpar_addr, &outlet);
  283. if (result) {
  284. pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
  285. __func__, __LINE__, ps3_result(result));
  286. return result;
  287. }
  288. result = ps3_alloc_irq(cpu, outlet, virq);
  289. BUG_ON(result);
  290. return result;
  291. }
  292. int ps3_free_vuart_irq(unsigned int virq)
  293. {
  294. int result;
  295. result = lv1_deconfigure_virtual_uart_irq();
  296. if (result) {
  297. pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
  298. __func__, __LINE__, ps3_result(result));
  299. return result;
  300. }
  301. ps3_free_irq(virq);
  302. return result;
  303. }
  304. /**
  305. * ps3_alloc_spe_irq - Configure an spe virq.
  306. * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
  307. * serviced on.
  308. * @spe_id: The spe_id returned from lv1_construct_logical_spe().
  309. * @class: The spe interrupt class {0,1,2}.
  310. * @virq: The assigned Linux virq.
  311. *
  312. */
  313. int ps3_alloc_spe_irq(enum ps3_cpu_binding cpu, unsigned long spe_id,
  314. unsigned int class, unsigned int *virq)
  315. {
  316. int result;
  317. unsigned long outlet;
  318. BUG_ON(class > 2);
  319. result = lv1_get_spe_irq_outlet(spe_id, class, &outlet);
  320. if (result) {
  321. pr_debug("%s:%d: lv1_get_spe_irq_outlet failed: %s\n",
  322. __func__, __LINE__, ps3_result(result));
  323. return result;
  324. }
  325. result = ps3_alloc_irq(cpu, outlet, virq);
  326. BUG_ON(result);
  327. return result;
  328. }
  329. int ps3_free_spe_irq(unsigned int virq)
  330. {
  331. ps3_free_irq(virq);
  332. return 0;
  333. }
  334. #define PS3_INVALID_OUTLET ((irq_hw_number_t)-1)
  335. #define PS3_PLUG_MAX 63
  336. #if defined(DEBUG)
  337. static void _dump_64_bmp(const char *header, const u64 *p, unsigned cpu,
  338. const char* func, int line)
  339. {
  340. pr_debug("%s:%d: %s %u {%04lx_%04lx_%04lx_%04lx}\n",
  341. func, line, header, cpu,
  342. *p >> 48, (*p >> 32) & 0xffff, (*p >> 16) & 0xffff,
  343. *p & 0xffff);
  344. }
  345. static void __attribute__ ((unused)) _dump_256_bmp(const char *header,
  346. const u64 *p, unsigned cpu, const char* func, int line)
  347. {
  348. pr_debug("%s:%d: %s %u {%016lx:%016lx:%016lx:%016lx}\n",
  349. func, line, header, cpu, p[0], p[1], p[2], p[3]);
  350. }
  351. #define dump_bmp(_x) _dump_bmp(_x, __func__, __LINE__)
  352. static void _dump_bmp(struct ps3_private* pd, const char* func, int line)
  353. {
  354. unsigned long flags;
  355. spin_lock_irqsave(&pd->bmp.lock, flags);
  356. _dump_64_bmp("stat", &pd->bmp.status, pd->cpu, func, line);
  357. _dump_64_bmp("mask", &pd->bmp.mask, pd->cpu, func, line);
  358. spin_unlock_irqrestore(&pd->bmp.lock, flags);
  359. }
  360. #define dump_mask(_x) _dump_mask(_x, __func__, __LINE__)
  361. static void __attribute__ ((unused)) _dump_mask(struct ps3_private* pd,
  362. const char* func, int line)
  363. {
  364. unsigned long flags;
  365. spin_lock_irqsave(&pd->bmp.lock, flags);
  366. _dump_64_bmp("mask", &pd->bmp.mask, pd->cpu, func, line);
  367. spin_unlock_irqrestore(&pd->bmp.lock, flags);
  368. }
  369. #else
  370. static void dump_bmp(struct ps3_private* pd) {};
  371. #endif /* defined(DEBUG) */
  372. static void ps3_chip_mask(unsigned int virq)
  373. {
  374. struct ps3_private *pd = get_irq_chip_data(virq);
  375. u64 bit = 0x8000000000000000UL >> virq;
  376. u64 *p = &pd->bmp.mask;
  377. u64 old;
  378. unsigned long flags;
  379. pr_debug("%s:%d: cpu %u, virq %d\n", __func__, __LINE__, pd->cpu, virq);
  380. local_irq_save(flags);
  381. asm volatile(
  382. "1: ldarx %0,0,%3\n"
  383. "andc %0,%0,%2\n"
  384. "stdcx. %0,0,%3\n"
  385. "bne- 1b"
  386. : "=&r" (old), "+m" (*p)
  387. : "r" (bit), "r" (p)
  388. : "cc" );
  389. lv1_did_update_interrupt_mask(pd->node, pd->cpu);
  390. local_irq_restore(flags);
  391. }
  392. static void ps3_chip_unmask(unsigned int virq)
  393. {
  394. struct ps3_private *pd = get_irq_chip_data(virq);
  395. u64 bit = 0x8000000000000000UL >> virq;
  396. u64 *p = &pd->bmp.mask;
  397. u64 old;
  398. unsigned long flags;
  399. pr_debug("%s:%d: cpu %u, virq %d\n", __func__, __LINE__, pd->cpu, virq);
  400. local_irq_save(flags);
  401. asm volatile(
  402. "1: ldarx %0,0,%3\n"
  403. "or %0,%0,%2\n"
  404. "stdcx. %0,0,%3\n"
  405. "bne- 1b"
  406. : "=&r" (old), "+m" (*p)
  407. : "r" (bit), "r" (p)
  408. : "cc" );
  409. lv1_did_update_interrupt_mask(pd->node, pd->cpu);
  410. local_irq_restore(flags);
  411. }
  412. static void ps3_chip_eoi(unsigned int virq)
  413. {
  414. const struct ps3_private *pd = get_irq_chip_data(virq);
  415. lv1_end_of_interrupt_ext(pd->node, pd->cpu, virq);
  416. }
  417. static struct irq_chip irq_chip = {
  418. .typename = "ps3",
  419. .mask = ps3_chip_mask,
  420. .unmask = ps3_chip_unmask,
  421. .eoi = ps3_chip_eoi,
  422. };
  423. static void ps3_host_unmap(struct irq_host *h, unsigned int virq)
  424. {
  425. set_irq_chip_data(virq, NULL);
  426. }
  427. static int ps3_host_map(struct irq_host *h, unsigned int virq,
  428. irq_hw_number_t hwirq)
  429. {
  430. pr_debug("%s:%d: hwirq %lu, virq %u\n", __func__, __LINE__, hwirq,
  431. virq);
  432. set_irq_chip_and_handler(virq, &irq_chip, handle_fasteoi_irq);
  433. return 0;
  434. }
  435. static struct irq_host_ops ps3_host_ops = {
  436. .map = ps3_host_map,
  437. .unmap = ps3_host_unmap,
  438. };
  439. void __init ps3_register_ipi_debug_brk(unsigned int cpu, unsigned int virq)
  440. {
  441. struct ps3_private *pd = &per_cpu(ps3_private, cpu);
  442. pd->bmp.ipi_debug_brk_mask = 0x8000000000000000UL >> virq;
  443. pr_debug("%s:%d: cpu %u, virq %u, mask %lxh\n", __func__, __LINE__,
  444. cpu, virq, pd->bmp.ipi_debug_brk_mask);
  445. }
  446. unsigned int ps3_get_irq(void)
  447. {
  448. struct ps3_private *pd = &__get_cpu_var(ps3_private);
  449. u64 x = (pd->bmp.status & pd->bmp.mask);
  450. unsigned int plug;
  451. /* check for ipi break first to stop this cpu ASAP */
  452. if (x & pd->bmp.ipi_debug_brk_mask)
  453. x &= pd->bmp.ipi_debug_brk_mask;
  454. asm volatile("cntlzd %0,%1" : "=r" (plug) : "r" (x));
  455. plug &= 0x3f;
  456. if (unlikely(plug) == NO_IRQ) {
  457. pr_debug("%s:%d: no plug found: cpu %u\n", __func__, __LINE__,
  458. pd->cpu);
  459. dump_bmp(&per_cpu(ps3_private, 0));
  460. dump_bmp(&per_cpu(ps3_private, 1));
  461. return NO_IRQ;
  462. }
  463. #if defined(DEBUG)
  464. if (unlikely(plug < NUM_ISA_INTERRUPTS || plug > PS3_PLUG_MAX)) {
  465. dump_bmp(&per_cpu(ps3_private, 0));
  466. dump_bmp(&per_cpu(ps3_private, 1));
  467. BUG();
  468. }
  469. #endif
  470. return plug;
  471. }
  472. void __init ps3_init_IRQ(void)
  473. {
  474. int result;
  475. unsigned cpu;
  476. struct irq_host *host;
  477. host = irq_alloc_host(IRQ_HOST_MAP_NOMAP, 0, &ps3_host_ops,
  478. PS3_INVALID_OUTLET);
  479. irq_set_default_host(host);
  480. irq_set_virq_count(PS3_PLUG_MAX + 1);
  481. for_each_possible_cpu(cpu) {
  482. struct ps3_private *pd = &per_cpu(ps3_private, cpu);
  483. lv1_get_logical_ppe_id(&pd->node);
  484. pd->cpu = get_hard_smp_processor_id(cpu);
  485. spin_lock_init(&pd->bmp.lock);
  486. pr_debug("%s:%d: node %lu, cpu %d, bmp %lxh\n", __func__,
  487. __LINE__, pd->node, pd->cpu,
  488. ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
  489. result = lv1_configure_irq_state_bitmap(pd->node, pd->cpu,
  490. ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
  491. if (result)
  492. pr_debug("%s:%d: lv1_configure_irq_state_bitmap failed:"
  493. " %s\n", __func__, __LINE__,
  494. ps3_result(result));
  495. }
  496. ppc_md.get_irq = ps3_get_irq;
  497. }