12832a81dSGeoff Levand /* 22832a81dSGeoff Levand * PS3 interrupt routines. 32832a81dSGeoff Levand * 42832a81dSGeoff Levand * Copyright (C) 2006 Sony Computer Entertainment Inc. 52832a81dSGeoff Levand * Copyright 2006 Sony Corp. 62832a81dSGeoff Levand * 72832a81dSGeoff Levand * This program is free software; you can redistribute it and/or modify 82832a81dSGeoff Levand * it under the terms of the GNU General Public License as published by 92832a81dSGeoff Levand * the Free Software Foundation; version 2 of the License. 102832a81dSGeoff Levand * 112832a81dSGeoff Levand * This program is distributed in the hope that it will be useful, 122832a81dSGeoff Levand * but WITHOUT ANY WARRANTY; without even the implied warranty of 132832a81dSGeoff Levand * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 142832a81dSGeoff Levand * GNU General Public License for more details. 152832a81dSGeoff Levand * 162832a81dSGeoff Levand * You should have received a copy of the GNU General Public License 172832a81dSGeoff Levand * along with this program; if not, write to the Free Software 182832a81dSGeoff Levand * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 192832a81dSGeoff Levand */ 202832a81dSGeoff Levand 212832a81dSGeoff Levand #include <linux/kernel.h> 222832a81dSGeoff Levand #include <linux/module.h> 232832a81dSGeoff Levand #include <linux/irq.h> 242832a81dSGeoff Levand 252832a81dSGeoff Levand #include <asm/machdep.h> 262832a81dSGeoff Levand #include <asm/udbg.h> 272832a81dSGeoff Levand #include <asm/lv1call.h> 2842d284bcSStephen Rothwell #include <asm/smp.h> 292832a81dSGeoff Levand 302832a81dSGeoff Levand #include "platform.h" 312832a81dSGeoff Levand 322832a81dSGeoff Levand #if defined(DEBUG) 332832a81dSGeoff Levand #define DBG(fmt...) udbg_printf(fmt) 342832a81dSGeoff Levand #else 352832a81dSGeoff Levand #define DBG(fmt...) do{if(0)printk(fmt);}while(0) 362832a81dSGeoff Levand #endif 372832a81dSGeoff Levand 382832a81dSGeoff Levand /** 39861be32cSGeoff Levand * struct ps3_bmp - a per cpu irq status and mask bitmap structure 40861be32cSGeoff Levand * @status: 256 bit status bitmap indexed by plug 41861be32cSGeoff Levand * @unused_1: 42861be32cSGeoff Levand * @mask: 256 bit mask bitmap indexed by plug 43861be32cSGeoff Levand * @unused_2: 44861be32cSGeoff Levand * @lock: 45861be32cSGeoff Levand * @ipi_debug_brk_mask: 46861be32cSGeoff Levand * 47861be32cSGeoff Levand * The HV mantains per SMT thread mappings of HV outlet to HV plug on 48861be32cSGeoff Levand * behalf of the guest. These mappings are implemented as 256 bit guest 49861be32cSGeoff Levand * supplied bitmaps indexed by plug number. The addresses of the bitmaps 50861be32cSGeoff Levand * are registered with the HV through lv1_configure_irq_state_bitmap(). 5157715765SGeoff Levand * The HV requires that the 512 bits of status + mask not cross a page 5257715765SGeoff Levand * boundary. PS3_BMP_MINALIGN is used to define this minimal 64 byte 5357715765SGeoff Levand * alignment. 54861be32cSGeoff Levand * 55861be32cSGeoff Levand * The HV supports 256 plugs per thread, assigned as {0..255}, for a total 56861be32cSGeoff Levand * of 512 plugs supported on a processor. To simplify the logic this 57861be32cSGeoff Levand * implementation equates HV plug value to Linux virq value, constrains each 58861be32cSGeoff Levand * interrupt to have a system wide unique plug number, and limits the range 59861be32cSGeoff Levand * of the plug values to map into the first dword of the bitmaps. This 60861be32cSGeoff Levand * gives a usable range of plug values of {NUM_ISA_INTERRUPTS..63}. Note 61861be32cSGeoff Levand * that there is no constraint on how many in this set an individual thread 62861be32cSGeoff Levand * can acquire. 63861be32cSGeoff Levand */ 64861be32cSGeoff Levand 6557715765SGeoff Levand #define PS3_BMP_MINALIGN 64 6657715765SGeoff Levand 67861be32cSGeoff Levand struct ps3_bmp { 68861be32cSGeoff Levand struct { 69861be32cSGeoff Levand u64 status; 70861be32cSGeoff Levand u64 unused_1[3]; 71861be32cSGeoff Levand u64 mask; 72861be32cSGeoff Levand u64 unused_2[3]; 73861be32cSGeoff Levand }; 74861be32cSGeoff Levand u64 ipi_debug_brk_mask; 75861be32cSGeoff Levand spinlock_t lock; 76861be32cSGeoff Levand }; 77861be32cSGeoff Levand 78861be32cSGeoff Levand /** 79861be32cSGeoff Levand * struct ps3_private - a per cpu data structure 80861be32cSGeoff Levand * @bmp: ps3_bmp structure 81861be32cSGeoff Levand * @node: HV logical_ppe_id 82861be32cSGeoff Levand * @cpu: HV thread_id 83861be32cSGeoff Levand */ 84861be32cSGeoff Levand 85861be32cSGeoff Levand struct ps3_private { 8657715765SGeoff Levand struct ps3_bmp bmp __attribute__ ((aligned (PS3_BMP_MINALIGN))); 87861be32cSGeoff Levand u64 node; 88861be32cSGeoff Levand unsigned int cpu; 89861be32cSGeoff Levand }; 90861be32cSGeoff Levand 91861be32cSGeoff Levand static DEFINE_PER_CPU(struct ps3_private, ps3_private); 92861be32cSGeoff Levand 93dc4f60c2SGeoff Levand /** 94dc4f60c2SGeoff Levand * ps3_virq_setup - virq related setup. 95dc4f60c2SGeoff Levand * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be 96dc4f60c2SGeoff Levand * serviced on. 97dc4f60c2SGeoff Levand * @outlet: The HV outlet from the various create outlet routines. 98dc4f60c2SGeoff Levand * @virq: The assigned Linux virq. 99dc4f60c2SGeoff Levand * 100dc4f60c2SGeoff Levand * Calls irq_create_mapping() to get a virq and sets the chip data to 101dc4f60c2SGeoff Levand * ps3_private data. 102dc4f60c2SGeoff Levand */ 103dc4f60c2SGeoff Levand 104dc4f60c2SGeoff Levand int ps3_virq_setup(enum ps3_cpu_binding cpu, unsigned long outlet, 105861be32cSGeoff Levand unsigned int *virq) 106861be32cSGeoff Levand { 107861be32cSGeoff Levand int result; 108861be32cSGeoff Levand struct ps3_private *pd; 109861be32cSGeoff Levand 110861be32cSGeoff Levand /* This defines the default interrupt distribution policy. */ 111861be32cSGeoff Levand 112861be32cSGeoff Levand if (cpu == PS3_BINDING_CPU_ANY) 113861be32cSGeoff Levand cpu = 0; 114861be32cSGeoff Levand 115861be32cSGeoff Levand pd = &per_cpu(ps3_private, cpu); 116861be32cSGeoff Levand 117861be32cSGeoff Levand *virq = irq_create_mapping(NULL, outlet); 118861be32cSGeoff Levand 119861be32cSGeoff Levand if (*virq == NO_IRQ) { 120861be32cSGeoff Levand pr_debug("%s:%d: irq_create_mapping failed: outlet %lu\n", 121861be32cSGeoff Levand __func__, __LINE__, outlet); 122861be32cSGeoff Levand result = -ENOMEM; 123861be32cSGeoff Levand goto fail_create; 124861be32cSGeoff Levand } 125861be32cSGeoff Levand 126861be32cSGeoff Levand pr_debug("%s:%d: outlet %lu => cpu %u, virq %u\n", __func__, __LINE__, 127861be32cSGeoff Levand outlet, cpu, *virq); 128861be32cSGeoff Levand 129861be32cSGeoff Levand result = set_irq_chip_data(*virq, pd); 130861be32cSGeoff Levand 131861be32cSGeoff Levand if (result) { 132861be32cSGeoff Levand pr_debug("%s:%d: set_irq_chip_data failed\n", 133861be32cSGeoff Levand __func__, __LINE__); 134861be32cSGeoff Levand goto fail_set; 135861be32cSGeoff Levand } 136861be32cSGeoff Levand 137861be32cSGeoff Levand return result; 138861be32cSGeoff Levand 139861be32cSGeoff Levand fail_set: 140861be32cSGeoff Levand irq_dispose_mapping(*virq); 141861be32cSGeoff Levand fail_create: 142861be32cSGeoff Levand return result; 143861be32cSGeoff Levand } 144861be32cSGeoff Levand 145dc4f60c2SGeoff Levand /** 146dc4f60c2SGeoff Levand * ps3_virq_destroy - virq related teardown. 147dc4f60c2SGeoff Levand * @virq: The assigned Linux virq. 148dc4f60c2SGeoff Levand * 149dc4f60c2SGeoff Levand * Clears chip data and calls irq_dispose_mapping() for the virq. 150dc4f60c2SGeoff Levand */ 151dc4f60c2SGeoff Levand 152dc4f60c2SGeoff Levand int ps3_virq_destroy(unsigned int virq) 153dc4f60c2SGeoff Levand { 154dc4f60c2SGeoff Levand const struct ps3_private *pd = get_irq_chip_data(virq); 155dc4f60c2SGeoff Levand 156dc4f60c2SGeoff Levand pr_debug("%s:%d: node %lu, cpu %d, virq %u\n", __func__, __LINE__, 157dc4f60c2SGeoff Levand pd->node, pd->cpu, virq); 158dc4f60c2SGeoff Levand 159dc4f60c2SGeoff Levand set_irq_chip_data(virq, NULL); 160dc4f60c2SGeoff Levand irq_dispose_mapping(virq); 161dc4f60c2SGeoff Levand 162dc4f60c2SGeoff Levand pr_debug("%s:%d <-\n", __func__, __LINE__); 163dc4f60c2SGeoff Levand return 0; 164dc4f60c2SGeoff Levand } 165dc4f60c2SGeoff Levand 166dc4f60c2SGeoff Levand /** 167dc4f60c2SGeoff Levand * ps3_irq_plug_setup - Generic outlet and virq related setup. 168dc4f60c2SGeoff Levand * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be 169dc4f60c2SGeoff Levand * serviced on. 170dc4f60c2SGeoff Levand * @outlet: The HV outlet from the various create outlet routines. 171dc4f60c2SGeoff Levand * @virq: The assigned Linux virq. 172dc4f60c2SGeoff Levand * 173dc4f60c2SGeoff Levand * Sets up virq and connects the irq plug. 174dc4f60c2SGeoff Levand */ 175dc4f60c2SGeoff Levand 176dc4f60c2SGeoff Levand int ps3_irq_plug_setup(enum ps3_cpu_binding cpu, unsigned long outlet, 177dc4f60c2SGeoff Levand unsigned int *virq) 178dc4f60c2SGeoff Levand { 179dc4f60c2SGeoff Levand int result; 180dc4f60c2SGeoff Levand struct ps3_private *pd; 181dc4f60c2SGeoff Levand 182dc4f60c2SGeoff Levand result = ps3_virq_setup(cpu, outlet, virq); 183dc4f60c2SGeoff Levand 184dc4f60c2SGeoff Levand if (result) { 185dc4f60c2SGeoff Levand pr_debug("%s:%d: ps3_virq_setup failed\n", __func__, __LINE__); 186dc4f60c2SGeoff Levand goto fail_setup; 187dc4f60c2SGeoff Levand } 188dc4f60c2SGeoff Levand 189dc4f60c2SGeoff Levand pd = get_irq_chip_data(*virq); 190dc4f60c2SGeoff Levand 191dc4f60c2SGeoff Levand /* Binds outlet to cpu + virq. */ 192dc4f60c2SGeoff Levand 193dc4f60c2SGeoff Levand result = lv1_connect_irq_plug_ext(pd->node, pd->cpu, *virq, outlet, 0); 194dc4f60c2SGeoff Levand 195dc4f60c2SGeoff Levand if (result) { 196dc4f60c2SGeoff Levand pr_info("%s:%d: lv1_connect_irq_plug_ext failed: %s\n", 197dc4f60c2SGeoff Levand __func__, __LINE__, ps3_result(result)); 198dc4f60c2SGeoff Levand result = -EPERM; 199dc4f60c2SGeoff Levand goto fail_connect; 200dc4f60c2SGeoff Levand } 201dc4f60c2SGeoff Levand 202dc4f60c2SGeoff Levand return result; 203dc4f60c2SGeoff Levand 204dc4f60c2SGeoff Levand fail_connect: 205dc4f60c2SGeoff Levand ps3_virq_destroy(*virq); 206dc4f60c2SGeoff Levand fail_setup: 207dc4f60c2SGeoff Levand return result; 208dc4f60c2SGeoff Levand } 209dc4f60c2SGeoff Levand EXPORT_SYMBOL_GPL(ps3_irq_plug_setup); 210dc4f60c2SGeoff Levand 211dc4f60c2SGeoff Levand /** 212dc4f60c2SGeoff Levand * ps3_irq_plug_destroy - Generic outlet and virq related teardown. 213dc4f60c2SGeoff Levand * @virq: The assigned Linux virq. 214dc4f60c2SGeoff Levand * 215dc4f60c2SGeoff Levand * Disconnects the irq plug and tears down virq. 216dc4f60c2SGeoff Levand * Do not call for system bus event interrupts setup with 217dc4f60c2SGeoff Levand * ps3_sb_event_receive_port_setup(). 218dc4f60c2SGeoff Levand */ 219dc4f60c2SGeoff Levand 220dc4f60c2SGeoff Levand int ps3_irq_plug_destroy(unsigned int virq) 221861be32cSGeoff Levand { 222861be32cSGeoff Levand int result; 223861be32cSGeoff Levand const struct ps3_private *pd = get_irq_chip_data(virq); 224861be32cSGeoff Levand 225861be32cSGeoff Levand pr_debug("%s:%d: node %lu, cpu %d, virq %u\n", __func__, __LINE__, 226861be32cSGeoff Levand pd->node, pd->cpu, virq); 227861be32cSGeoff Levand 228861be32cSGeoff Levand result = lv1_disconnect_irq_plug_ext(pd->node, pd->cpu, virq); 229861be32cSGeoff Levand 230861be32cSGeoff Levand if (result) 231861be32cSGeoff Levand pr_info("%s:%d: lv1_disconnect_irq_plug_ext failed: %s\n", 232861be32cSGeoff Levand __func__, __LINE__, ps3_result(result)); 233861be32cSGeoff Levand 234dc4f60c2SGeoff Levand ps3_virq_destroy(virq); 235dc4f60c2SGeoff Levand 236b1eeb38eSGeert Uytterhoeven return result; 237861be32cSGeoff Levand } 238dc4f60c2SGeoff Levand EXPORT_SYMBOL_GPL(ps3_irq_plug_destroy); 239861be32cSGeoff Levand 240861be32cSGeoff Levand /** 241dc4f60c2SGeoff Levand * ps3_event_receive_port_setup - Setup an event receive port. 242861be32cSGeoff Levand * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be 243861be32cSGeoff Levand * serviced on. 2442832a81dSGeoff Levand * @virq: The assigned Linux virq. 2452832a81dSGeoff Levand * 2462832a81dSGeoff Levand * The virq can be used with lv1_connect_interrupt_event_receive_port() to 247dc4f60c2SGeoff Levand * arrange to receive interrupts from system-bus devices, or with 248dc4f60c2SGeoff Levand * ps3_send_event_locally() to signal events. 2492832a81dSGeoff Levand */ 2502832a81dSGeoff Levand 251dc4f60c2SGeoff Levand int ps3_event_receive_port_setup(enum ps3_cpu_binding cpu, unsigned int *virq) 2522832a81dSGeoff Levand { 2532832a81dSGeoff Levand int result; 2542832a81dSGeoff Levand unsigned long outlet; 2552832a81dSGeoff Levand 2562832a81dSGeoff Levand result = lv1_construct_event_receive_port(&outlet); 2572832a81dSGeoff Levand 2582832a81dSGeoff Levand if (result) { 2592832a81dSGeoff Levand pr_debug("%s:%d: lv1_construct_event_receive_port failed: %s\n", 2602832a81dSGeoff Levand __func__, __LINE__, ps3_result(result)); 2612832a81dSGeoff Levand *virq = NO_IRQ; 2622832a81dSGeoff Levand return result; 2632832a81dSGeoff Levand } 2642832a81dSGeoff Levand 265dc4f60c2SGeoff Levand result = ps3_irq_plug_setup(cpu, outlet, virq); 266861be32cSGeoff Levand BUG_ON(result); 2672832a81dSGeoff Levand 268861be32cSGeoff Levand return result; 2692832a81dSGeoff Levand } 270dc4f60c2SGeoff Levand EXPORT_SYMBOL_GPL(ps3_event_receive_port_setup); 2712832a81dSGeoff Levand 272dc4f60c2SGeoff Levand /** 273dc4f60c2SGeoff Levand * ps3_event_receive_port_destroy - Destroy an event receive port. 274dc4f60c2SGeoff Levand * @virq: The assigned Linux virq. 275dc4f60c2SGeoff Levand * 276dc4f60c2SGeoff Levand * Since ps3_event_receive_port_destroy destroys the receive port outlet, 277dc4f60c2SGeoff Levand * SB devices need to call disconnect_interrupt_event_receive_port() before 278dc4f60c2SGeoff Levand * this. 279dc4f60c2SGeoff Levand */ 280dc4f60c2SGeoff Levand 281dc4f60c2SGeoff Levand int ps3_event_receive_port_destroy(unsigned int virq) 2822832a81dSGeoff Levand { 2832832a81dSGeoff Levand int result; 2842832a81dSGeoff Levand 285dc4f60c2SGeoff Levand pr_debug(" -> %s:%d virq: %u\n", __func__, __LINE__, virq); 2862832a81dSGeoff Levand 2872832a81dSGeoff Levand result = lv1_destruct_event_receive_port(virq_to_hw(virq)); 2882832a81dSGeoff Levand 2892832a81dSGeoff Levand if (result) 2902832a81dSGeoff Levand pr_debug("%s:%d: lv1_destruct_event_receive_port failed: %s\n", 2912832a81dSGeoff Levand __func__, __LINE__, ps3_result(result)); 2922832a81dSGeoff Levand 293dc4f60c2SGeoff Levand /* lv1_destruct_event_receive_port() destroys the IRQ plug, 294dc4f60c2SGeoff Levand * so don't call ps3_irq_plug_destroy() here. 295dc4f60c2SGeoff Levand */ 296dc4f60c2SGeoff Levand 297dc4f60c2SGeoff Levand result = ps3_virq_destroy(virq); 298dc4f60c2SGeoff Levand BUG_ON(result); 2992832a81dSGeoff Levand 3002832a81dSGeoff Levand pr_debug(" <- %s:%d\n", __func__, __LINE__); 3012832a81dSGeoff Levand return result; 3022832a81dSGeoff Levand } 303dc4f60c2SGeoff Levand EXPORT_SYMBOL_GPL(ps3_event_receive_port_destroy); 3042832a81dSGeoff Levand 3052832a81dSGeoff Levand int ps3_send_event_locally(unsigned int virq) 3062832a81dSGeoff Levand { 3072832a81dSGeoff Levand return lv1_send_event_locally(virq_to_hw(virq)); 3082832a81dSGeoff Levand } 3092832a81dSGeoff Levand 3102832a81dSGeoff Levand /** 311dc4f60c2SGeoff Levand * ps3_sb_event_receive_port_setup - Setup a system bus event receive port. 312861be32cSGeoff Levand * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be 313861be32cSGeoff Levand * serviced on. 3142832a81dSGeoff Levand * @did: The HV device identifier read from the system repository. 3152832a81dSGeoff Levand * @interrupt_id: The device interrupt id read from the system repository. 3162832a81dSGeoff Levand * @virq: The assigned Linux virq. 3172832a81dSGeoff Levand * 3182832a81dSGeoff Levand * An event irq represents a virtual device interrupt. The interrupt_id 3192832a81dSGeoff Levand * coresponds to the software interrupt number. 3202832a81dSGeoff Levand */ 3212832a81dSGeoff Levand 322dc4f60c2SGeoff Levand int ps3_sb_event_receive_port_setup(enum ps3_cpu_binding cpu, 323861be32cSGeoff Levand const struct ps3_device_id *did, unsigned int interrupt_id, 324861be32cSGeoff Levand unsigned int *virq) 3252832a81dSGeoff Levand { 326dc4f60c2SGeoff Levand /* this should go in system-bus.c */ 327dc4f60c2SGeoff Levand 3282832a81dSGeoff Levand int result; 3292832a81dSGeoff Levand 330dc4f60c2SGeoff Levand result = ps3_event_receive_port_setup(cpu, virq); 3312832a81dSGeoff Levand 3322832a81dSGeoff Levand if (result) 3332832a81dSGeoff Levand return result; 3342832a81dSGeoff Levand 3352832a81dSGeoff Levand result = lv1_connect_interrupt_event_receive_port(did->bus_id, 3362832a81dSGeoff Levand did->dev_id, virq_to_hw(*virq), interrupt_id); 3372832a81dSGeoff Levand 3382832a81dSGeoff Levand if (result) { 3392832a81dSGeoff Levand pr_debug("%s:%d: lv1_connect_interrupt_event_receive_port" 3402832a81dSGeoff Levand " failed: %s\n", __func__, __LINE__, 3412832a81dSGeoff Levand ps3_result(result)); 342dc4f60c2SGeoff Levand ps3_event_receive_port_destroy(*virq); 3432832a81dSGeoff Levand *virq = NO_IRQ; 3442832a81dSGeoff Levand return result; 3452832a81dSGeoff Levand } 3462832a81dSGeoff Levand 3472832a81dSGeoff Levand pr_debug("%s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__, 3482832a81dSGeoff Levand interrupt_id, *virq); 3492832a81dSGeoff Levand 3502832a81dSGeoff Levand return 0; 3512832a81dSGeoff Levand } 352dc4f60c2SGeoff Levand EXPORT_SYMBOL(ps3_sb_event_receive_port_setup); 3532832a81dSGeoff Levand 354dc4f60c2SGeoff Levand int ps3_sb_event_receive_port_destroy(const struct ps3_device_id *did, 3552832a81dSGeoff Levand unsigned int interrupt_id, unsigned int virq) 3562832a81dSGeoff Levand { 357dc4f60c2SGeoff Levand /* this should go in system-bus.c */ 358dc4f60c2SGeoff Levand 3592832a81dSGeoff Levand int result; 3602832a81dSGeoff Levand 3612832a81dSGeoff Levand pr_debug(" -> %s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__, 3622832a81dSGeoff Levand interrupt_id, virq); 3632832a81dSGeoff Levand 3642832a81dSGeoff Levand result = lv1_disconnect_interrupt_event_receive_port(did->bus_id, 3652832a81dSGeoff Levand did->dev_id, virq_to_hw(virq), interrupt_id); 3662832a81dSGeoff Levand 3672832a81dSGeoff Levand if (result) 3682832a81dSGeoff Levand pr_debug("%s:%d: lv1_disconnect_interrupt_event_receive_port" 3692832a81dSGeoff Levand " failed: %s\n", __func__, __LINE__, 3702832a81dSGeoff Levand ps3_result(result)); 3712832a81dSGeoff Levand 372dc4f60c2SGeoff Levand result = ps3_event_receive_port_destroy(virq); 373dc4f60c2SGeoff Levand BUG_ON(result); 3742832a81dSGeoff Levand 3752832a81dSGeoff Levand pr_debug(" <- %s:%d\n", __func__, __LINE__); 3762832a81dSGeoff Levand return result; 3772832a81dSGeoff Levand } 378dc4f60c2SGeoff Levand EXPORT_SYMBOL(ps3_sb_event_receive_port_destroy); 3792832a81dSGeoff Levand 3802832a81dSGeoff Levand /** 381dc4f60c2SGeoff Levand * ps3_io_irq_setup - Setup a system bus io irq. 382dc4f60c2SGeoff Levand * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be 383dc4f60c2SGeoff Levand * serviced on. 384dc4f60c2SGeoff Levand * @interrupt_id: The device interrupt id read from the system repository. 385dc4f60c2SGeoff Levand * @virq: The assigned Linux virq. 386dc4f60c2SGeoff Levand * 387dc4f60c2SGeoff Levand * An io irq represents a non-virtualized device interrupt. interrupt_id 388dc4f60c2SGeoff Levand * coresponds to the interrupt number of the interrupt controller. 389dc4f60c2SGeoff Levand */ 390dc4f60c2SGeoff Levand 391dc4f60c2SGeoff Levand int ps3_io_irq_setup(enum ps3_cpu_binding cpu, unsigned int interrupt_id, 392dc4f60c2SGeoff Levand unsigned int *virq) 393dc4f60c2SGeoff Levand { 394dc4f60c2SGeoff Levand int result; 395dc4f60c2SGeoff Levand unsigned long outlet; 396dc4f60c2SGeoff Levand 397dc4f60c2SGeoff Levand result = lv1_construct_io_irq_outlet(interrupt_id, &outlet); 398dc4f60c2SGeoff Levand 399dc4f60c2SGeoff Levand if (result) { 400dc4f60c2SGeoff Levand pr_debug("%s:%d: lv1_construct_io_irq_outlet failed: %s\n", 401dc4f60c2SGeoff Levand __func__, __LINE__, ps3_result(result)); 402dc4f60c2SGeoff Levand return result; 403dc4f60c2SGeoff Levand } 404dc4f60c2SGeoff Levand 405dc4f60c2SGeoff Levand result = ps3_irq_plug_setup(cpu, outlet, virq); 406dc4f60c2SGeoff Levand BUG_ON(result); 407dc4f60c2SGeoff Levand 408dc4f60c2SGeoff Levand return result; 409dc4f60c2SGeoff Levand } 410dc4f60c2SGeoff Levand EXPORT_SYMBOL_GPL(ps3_io_irq_setup); 411dc4f60c2SGeoff Levand 412dc4f60c2SGeoff Levand int ps3_io_irq_destroy(unsigned int virq) 413dc4f60c2SGeoff Levand { 414dc4f60c2SGeoff Levand int result; 415dc4f60c2SGeoff Levand 416dc4f60c2SGeoff Levand result = lv1_destruct_io_irq_outlet(virq_to_hw(virq)); 417dc4f60c2SGeoff Levand 418dc4f60c2SGeoff Levand if (result) 419dc4f60c2SGeoff Levand pr_debug("%s:%d: lv1_destruct_io_irq_outlet failed: %s\n", 420dc4f60c2SGeoff Levand __func__, __LINE__, ps3_result(result)); 421dc4f60c2SGeoff Levand 422dc4f60c2SGeoff Levand result = ps3_irq_plug_destroy(virq); 423dc4f60c2SGeoff Levand BUG_ON(result); 424dc4f60c2SGeoff Levand 425dc4f60c2SGeoff Levand return result; 426dc4f60c2SGeoff Levand } 427dc4f60c2SGeoff Levand EXPORT_SYMBOL_GPL(ps3_io_irq_destroy); 428dc4f60c2SGeoff Levand 429dc4f60c2SGeoff Levand /** 430dc4f60c2SGeoff Levand * ps3_vuart_irq_setup - Setup the system virtual uart virq. 431861be32cSGeoff Levand * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be 432861be32cSGeoff Levand * serviced on. 4332832a81dSGeoff Levand * @virt_addr_bmp: The caller supplied virtual uart interrupt bitmap. 4342832a81dSGeoff Levand * @virq: The assigned Linux virq. 4352832a81dSGeoff Levand * 4362832a81dSGeoff Levand * The system supports only a single virtual uart, so multiple calls without 4372832a81dSGeoff Levand * freeing the interrupt will return a wrong state error. 4382832a81dSGeoff Levand */ 4392832a81dSGeoff Levand 440dc4f60c2SGeoff Levand int ps3_vuart_irq_setup(enum ps3_cpu_binding cpu, void* virt_addr_bmp, 441861be32cSGeoff Levand unsigned int *virq) 4422832a81dSGeoff Levand { 4432832a81dSGeoff Levand int result; 4442832a81dSGeoff Levand unsigned long outlet; 445861be32cSGeoff Levand u64 lpar_addr; 4462832a81dSGeoff Levand 447861be32cSGeoff Levand BUG_ON(!is_kernel_addr((u64)virt_addr_bmp)); 4482832a81dSGeoff Levand 4492832a81dSGeoff Levand lpar_addr = ps3_mm_phys_to_lpar(__pa(virt_addr_bmp)); 4502832a81dSGeoff Levand 4512832a81dSGeoff Levand result = lv1_configure_virtual_uart_irq(lpar_addr, &outlet); 4522832a81dSGeoff Levand 4532832a81dSGeoff Levand if (result) { 4542832a81dSGeoff Levand pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n", 4552832a81dSGeoff Levand __func__, __LINE__, ps3_result(result)); 4562832a81dSGeoff Levand return result; 4572832a81dSGeoff Levand } 4582832a81dSGeoff Levand 459dc4f60c2SGeoff Levand result = ps3_irq_plug_setup(cpu, outlet, virq); 460861be32cSGeoff Levand BUG_ON(result); 4612832a81dSGeoff Levand 462861be32cSGeoff Levand return result; 4632832a81dSGeoff Levand } 4642832a81dSGeoff Levand 465dc4f60c2SGeoff Levand int ps3_vuart_irq_destroy(unsigned int virq) 4662832a81dSGeoff Levand { 4672832a81dSGeoff Levand int result; 4682832a81dSGeoff Levand 4692832a81dSGeoff Levand result = lv1_deconfigure_virtual_uart_irq(); 4702832a81dSGeoff Levand 4712832a81dSGeoff Levand if (result) { 4722832a81dSGeoff Levand pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n", 4732832a81dSGeoff Levand __func__, __LINE__, ps3_result(result)); 4742832a81dSGeoff Levand return result; 4752832a81dSGeoff Levand } 4762832a81dSGeoff Levand 477dc4f60c2SGeoff Levand result = ps3_irq_plug_destroy(virq); 478dc4f60c2SGeoff Levand BUG_ON(result); 4792832a81dSGeoff Levand 4802832a81dSGeoff Levand return result; 4812832a81dSGeoff Levand } 4822832a81dSGeoff Levand 4832832a81dSGeoff Levand /** 484dc4f60c2SGeoff Levand * ps3_spe_irq_setup - Setup an spe virq. 485861be32cSGeoff Levand * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be 486861be32cSGeoff Levand * serviced on. 4872832a81dSGeoff Levand * @spe_id: The spe_id returned from lv1_construct_logical_spe(). 4882832a81dSGeoff Levand * @class: The spe interrupt class {0,1,2}. 4892832a81dSGeoff Levand * @virq: The assigned Linux virq. 4902832a81dSGeoff Levand * 4912832a81dSGeoff Levand */ 4922832a81dSGeoff Levand 493dc4f60c2SGeoff Levand int ps3_spe_irq_setup(enum ps3_cpu_binding cpu, unsigned long spe_id, 494861be32cSGeoff Levand unsigned int class, unsigned int *virq) 4952832a81dSGeoff Levand { 4962832a81dSGeoff Levand int result; 4972832a81dSGeoff Levand unsigned long outlet; 4982832a81dSGeoff Levand 4992832a81dSGeoff Levand BUG_ON(class > 2); 5002832a81dSGeoff Levand 5012832a81dSGeoff Levand result = lv1_get_spe_irq_outlet(spe_id, class, &outlet); 5022832a81dSGeoff Levand 5032832a81dSGeoff Levand if (result) { 5042832a81dSGeoff Levand pr_debug("%s:%d: lv1_get_spe_irq_outlet failed: %s\n", 5052832a81dSGeoff Levand __func__, __LINE__, ps3_result(result)); 5062832a81dSGeoff Levand return result; 5072832a81dSGeoff Levand } 5082832a81dSGeoff Levand 509dc4f60c2SGeoff Levand result = ps3_irq_plug_setup(cpu, outlet, virq); 510861be32cSGeoff Levand BUG_ON(result); 5112832a81dSGeoff Levand 512861be32cSGeoff Levand return result; 5132832a81dSGeoff Levand } 5142832a81dSGeoff Levand 515dc4f60c2SGeoff Levand int ps3_spe_irq_destroy(unsigned int virq) 5162832a81dSGeoff Levand { 517dc4f60c2SGeoff Levand int result = ps3_irq_plug_destroy(virq); 518dc4f60c2SGeoff Levand BUG_ON(result); 5192832a81dSGeoff Levand return 0; 5202832a81dSGeoff Levand } 5212832a81dSGeoff Levand 522b1eeb38eSGeert Uytterhoeven 5232832a81dSGeoff Levand #define PS3_INVALID_OUTLET ((irq_hw_number_t)-1) 5242832a81dSGeoff Levand #define PS3_PLUG_MAX 63 5252832a81dSGeoff Levand 5262832a81dSGeoff Levand #if defined(DEBUG) 527861be32cSGeoff Levand static void _dump_64_bmp(const char *header, const u64 *p, unsigned cpu, 5282832a81dSGeoff Levand const char* func, int line) 5292832a81dSGeoff Levand { 5302832a81dSGeoff Levand pr_debug("%s:%d: %s %u {%04lx_%04lx_%04lx_%04lx}\n", 5312832a81dSGeoff Levand func, line, header, cpu, 5322832a81dSGeoff Levand *p >> 48, (*p >> 32) & 0xffff, (*p >> 16) & 0xffff, 5332832a81dSGeoff Levand *p & 0xffff); 5342832a81dSGeoff Levand } 5352832a81dSGeoff Levand 536*848cfdc5SGeoff Levand static void __maybe_unused _dump_256_bmp(const char *header, 537861be32cSGeoff Levand const u64 *p, unsigned cpu, const char* func, int line) 5382832a81dSGeoff Levand { 5392832a81dSGeoff Levand pr_debug("%s:%d: %s %u {%016lx:%016lx:%016lx:%016lx}\n", 5402832a81dSGeoff Levand func, line, header, cpu, p[0], p[1], p[2], p[3]); 5412832a81dSGeoff Levand } 5422832a81dSGeoff Levand 5432832a81dSGeoff Levand #define dump_bmp(_x) _dump_bmp(_x, __func__, __LINE__) 5449633ac8dSGeoff Levand static void _dump_bmp(struct ps3_private* pd, const char* func, int line) 5452832a81dSGeoff Levand { 5462832a81dSGeoff Levand unsigned long flags; 5472832a81dSGeoff Levand 5482832a81dSGeoff Levand spin_lock_irqsave(&pd->bmp.lock, flags); 5492832a81dSGeoff Levand _dump_64_bmp("stat", &pd->bmp.status, pd->cpu, func, line); 5502832a81dSGeoff Levand _dump_64_bmp("mask", &pd->bmp.mask, pd->cpu, func, line); 5512832a81dSGeoff Levand spin_unlock_irqrestore(&pd->bmp.lock, flags); 5522832a81dSGeoff Levand } 5532832a81dSGeoff Levand 5542832a81dSGeoff Levand #define dump_mask(_x) _dump_mask(_x, __func__, __LINE__) 555*848cfdc5SGeoff Levand static void __maybe_unused _dump_mask(struct ps3_private *pd, 5562832a81dSGeoff Levand const char* func, int line) 5572832a81dSGeoff Levand { 5582832a81dSGeoff Levand unsigned long flags; 5592832a81dSGeoff Levand 5602832a81dSGeoff Levand spin_lock_irqsave(&pd->bmp.lock, flags); 5612832a81dSGeoff Levand _dump_64_bmp("mask", &pd->bmp.mask, pd->cpu, func, line); 5622832a81dSGeoff Levand spin_unlock_irqrestore(&pd->bmp.lock, flags); 5632832a81dSGeoff Levand } 5642832a81dSGeoff Levand #else 5659633ac8dSGeoff Levand static void dump_bmp(struct ps3_private* pd) {}; 5662832a81dSGeoff Levand #endif /* defined(DEBUG) */ 5672832a81dSGeoff Levand 5689633ac8dSGeoff Levand static void ps3_chip_mask(unsigned int virq) 5692832a81dSGeoff Levand { 5709633ac8dSGeoff Levand struct ps3_private *pd = get_irq_chip_data(virq); 571861be32cSGeoff Levand u64 bit = 0x8000000000000000UL >> virq; 572861be32cSGeoff Levand u64 *p = &pd->bmp.mask; 573861be32cSGeoff Levand u64 old; 5749cf9e196SBenjamin Herrenschmidt unsigned long flags; 5752832a81dSGeoff Levand 5762832a81dSGeoff Levand pr_debug("%s:%d: cpu %u, virq %d\n", __func__, __LINE__, pd->cpu, virq); 5772832a81dSGeoff Levand 5789cf9e196SBenjamin Herrenschmidt local_irq_save(flags); 5799cf9e196SBenjamin Herrenschmidt asm volatile( 5809cf9e196SBenjamin Herrenschmidt "1: ldarx %0,0,%3\n" 5819cf9e196SBenjamin Herrenschmidt "andc %0,%0,%2\n" 5829cf9e196SBenjamin Herrenschmidt "stdcx. %0,0,%3\n" 5839cf9e196SBenjamin Herrenschmidt "bne- 1b" 5849cf9e196SBenjamin Herrenschmidt : "=&r" (old), "+m" (*p) 5859cf9e196SBenjamin Herrenschmidt : "r" (bit), "r" (p) 5869cf9e196SBenjamin Herrenschmidt : "cc" ); 5872832a81dSGeoff Levand 5882832a81dSGeoff Levand lv1_did_update_interrupt_mask(pd->node, pd->cpu); 5899cf9e196SBenjamin Herrenschmidt local_irq_restore(flags); 5902832a81dSGeoff Levand } 5912832a81dSGeoff Levand 5929633ac8dSGeoff Levand static void ps3_chip_unmask(unsigned int virq) 5932832a81dSGeoff Levand { 5949633ac8dSGeoff Levand struct ps3_private *pd = get_irq_chip_data(virq); 595861be32cSGeoff Levand u64 bit = 0x8000000000000000UL >> virq; 596861be32cSGeoff Levand u64 *p = &pd->bmp.mask; 597861be32cSGeoff Levand u64 old; 5989cf9e196SBenjamin Herrenschmidt unsigned long flags; 5992832a81dSGeoff Levand 6002832a81dSGeoff Levand pr_debug("%s:%d: cpu %u, virq %d\n", __func__, __LINE__, pd->cpu, virq); 6012832a81dSGeoff Levand 6029cf9e196SBenjamin Herrenschmidt local_irq_save(flags); 6039cf9e196SBenjamin Herrenschmidt asm volatile( 6049cf9e196SBenjamin Herrenschmidt "1: ldarx %0,0,%3\n" 6059cf9e196SBenjamin Herrenschmidt "or %0,%0,%2\n" 6069cf9e196SBenjamin Herrenschmidt "stdcx. %0,0,%3\n" 6079cf9e196SBenjamin Herrenschmidt "bne- 1b" 6089cf9e196SBenjamin Herrenschmidt : "=&r" (old), "+m" (*p) 6099cf9e196SBenjamin Herrenschmidt : "r" (bit), "r" (p) 6109cf9e196SBenjamin Herrenschmidt : "cc" ); 6112832a81dSGeoff Levand 6122832a81dSGeoff Levand lv1_did_update_interrupt_mask(pd->node, pd->cpu); 6139cf9e196SBenjamin Herrenschmidt local_irq_restore(flags); 6142832a81dSGeoff Levand } 6152832a81dSGeoff Levand 6169633ac8dSGeoff Levand static void ps3_chip_eoi(unsigned int virq) 6172832a81dSGeoff Levand { 618407e24a0SGeoff Levand const struct ps3_private *pd = get_irq_chip_data(virq); 619407e24a0SGeoff Levand lv1_end_of_interrupt_ext(pd->node, pd->cpu, virq); 6202832a81dSGeoff Levand } 6212832a81dSGeoff Levand 6222832a81dSGeoff Levand static struct irq_chip irq_chip = { 6232832a81dSGeoff Levand .typename = "ps3", 6249633ac8dSGeoff Levand .mask = ps3_chip_mask, 6259633ac8dSGeoff Levand .unmask = ps3_chip_unmask, 6269633ac8dSGeoff Levand .eoi = ps3_chip_eoi, 6272832a81dSGeoff Levand }; 6282832a81dSGeoff Levand 6299633ac8dSGeoff Levand static void ps3_host_unmap(struct irq_host *h, unsigned int virq) 6302832a81dSGeoff Levand { 631861be32cSGeoff Levand set_irq_chip_data(virq, NULL); 6322832a81dSGeoff Levand } 6332832a81dSGeoff Levand 6349633ac8dSGeoff Levand static int ps3_host_map(struct irq_host *h, unsigned int virq, 6352832a81dSGeoff Levand irq_hw_number_t hwirq) 6362832a81dSGeoff Levand { 637861be32cSGeoff Levand pr_debug("%s:%d: hwirq %lu, virq %u\n", __func__, __LINE__, hwirq, 638861be32cSGeoff Levand virq); 6392832a81dSGeoff Levand 6402832a81dSGeoff Levand set_irq_chip_and_handler(virq, &irq_chip, handle_fasteoi_irq); 6412832a81dSGeoff Levand 642861be32cSGeoff Levand return 0; 6432832a81dSGeoff Levand } 6442832a81dSGeoff Levand 6459633ac8dSGeoff Levand static struct irq_host_ops ps3_host_ops = { 6469633ac8dSGeoff Levand .map = ps3_host_map, 6479633ac8dSGeoff Levand .unmap = ps3_host_unmap, 6482832a81dSGeoff Levand }; 6492832a81dSGeoff Levand 6502832a81dSGeoff Levand void __init ps3_register_ipi_debug_brk(unsigned int cpu, unsigned int virq) 6512832a81dSGeoff Levand { 6529633ac8dSGeoff Levand struct ps3_private *pd = &per_cpu(ps3_private, cpu); 6532832a81dSGeoff Levand 6542832a81dSGeoff Levand pd->bmp.ipi_debug_brk_mask = 0x8000000000000000UL >> virq; 6552832a81dSGeoff Levand 6562832a81dSGeoff Levand pr_debug("%s:%d: cpu %u, virq %u, mask %lxh\n", __func__, __LINE__, 6572832a81dSGeoff Levand cpu, virq, pd->bmp.ipi_debug_brk_mask); 6582832a81dSGeoff Levand } 6592832a81dSGeoff Levand 6609cf9e196SBenjamin Herrenschmidt unsigned int ps3_get_irq(void) 6612832a81dSGeoff Levand { 6629cf9e196SBenjamin Herrenschmidt struct ps3_private *pd = &__get_cpu_var(ps3_private); 663861be32cSGeoff Levand u64 x = (pd->bmp.status & pd->bmp.mask); 6649cf9e196SBenjamin Herrenschmidt unsigned int plug; 6652832a81dSGeoff Levand 6662832a81dSGeoff Levand /* check for ipi break first to stop this cpu ASAP */ 6672832a81dSGeoff Levand 6689cf9e196SBenjamin Herrenschmidt if (x & pd->bmp.ipi_debug_brk_mask) 6699cf9e196SBenjamin Herrenschmidt x &= pd->bmp.ipi_debug_brk_mask; 6702832a81dSGeoff Levand 6719cf9e196SBenjamin Herrenschmidt asm volatile("cntlzd %0,%1" : "=r" (plug) : "r" (x)); 6729cf9e196SBenjamin Herrenschmidt plug &= 0x3f; 6732832a81dSGeoff Levand 6749cf9e196SBenjamin Herrenschmidt if (unlikely(plug) == NO_IRQ) { 6752832a81dSGeoff Levand pr_debug("%s:%d: no plug found: cpu %u\n", __func__, __LINE__, 6762832a81dSGeoff Levand pd->cpu); 6779633ac8dSGeoff Levand dump_bmp(&per_cpu(ps3_private, 0)); 6789633ac8dSGeoff Levand dump_bmp(&per_cpu(ps3_private, 1)); 6792832a81dSGeoff Levand return NO_IRQ; 6802832a81dSGeoff Levand } 6812832a81dSGeoff Levand 6822832a81dSGeoff Levand #if defined(DEBUG) 6839cf9e196SBenjamin Herrenschmidt if (unlikely(plug < NUM_ISA_INTERRUPTS || plug > PS3_PLUG_MAX)) { 6849633ac8dSGeoff Levand dump_bmp(&per_cpu(ps3_private, 0)); 6859633ac8dSGeoff Levand dump_bmp(&per_cpu(ps3_private, 1)); 6862832a81dSGeoff Levand BUG(); 6872832a81dSGeoff Levand } 6882832a81dSGeoff Levand #endif 6892832a81dSGeoff Levand return plug; 6902832a81dSGeoff Levand } 6912832a81dSGeoff Levand 6922832a81dSGeoff Levand void __init ps3_init_IRQ(void) 6932832a81dSGeoff Levand { 6942832a81dSGeoff Levand int result; 6952832a81dSGeoff Levand unsigned cpu; 6962832a81dSGeoff Levand struct irq_host *host; 6972832a81dSGeoff Levand 6989633ac8dSGeoff Levand host = irq_alloc_host(IRQ_HOST_MAP_NOMAP, 0, &ps3_host_ops, 6992832a81dSGeoff Levand PS3_INVALID_OUTLET); 7002832a81dSGeoff Levand irq_set_default_host(host); 7012832a81dSGeoff Levand irq_set_virq_count(PS3_PLUG_MAX + 1); 7022832a81dSGeoff Levand 7032832a81dSGeoff Levand for_each_possible_cpu(cpu) { 7049633ac8dSGeoff Levand struct ps3_private *pd = &per_cpu(ps3_private, cpu); 7052832a81dSGeoff Levand 706407e24a0SGeoff Levand lv1_get_logical_ppe_id(&pd->node); 707407e24a0SGeoff Levand pd->cpu = get_hard_smp_processor_id(cpu); 7082832a81dSGeoff Levand spin_lock_init(&pd->bmp.lock); 7092832a81dSGeoff Levand 710407e24a0SGeoff Levand pr_debug("%s:%d: node %lu, cpu %d, bmp %lxh\n", __func__, 711407e24a0SGeoff Levand __LINE__, pd->node, pd->cpu, 712407e24a0SGeoff Levand ps3_mm_phys_to_lpar(__pa(&pd->bmp))); 713407e24a0SGeoff Levand 714407e24a0SGeoff Levand result = lv1_configure_irq_state_bitmap(pd->node, pd->cpu, 715407e24a0SGeoff Levand ps3_mm_phys_to_lpar(__pa(&pd->bmp))); 7162832a81dSGeoff Levand 7172832a81dSGeoff Levand if (result) 7182832a81dSGeoff Levand pr_debug("%s:%d: lv1_configure_irq_state_bitmap failed:" 7192832a81dSGeoff Levand " %s\n", __func__, __LINE__, 7202832a81dSGeoff Levand ps3_result(result)); 7212832a81dSGeoff Levand } 7222832a81dSGeoff Levand 7232832a81dSGeoff Levand ppc_md.get_irq = ps3_get_irq; 7242832a81dSGeoff Levand } 725