| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright 2010 Tilera Corporation. All Rights Reserved. | 
 | 3 |  * | 
 | 4 |  *   This program is free software; you can redistribute it and/or | 
 | 5 |  *   modify it under the terms of the GNU General Public License | 
 | 6 |  *   as published by the Free Software Foundation, version 2. | 
 | 7 |  * | 
 | 8 |  *   This program is distributed in the hope that it will be useful, but | 
 | 9 |  *   WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 10 |  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | 
 | 11 |  *   NON INFRINGEMENT.  See the GNU General Public License for | 
 | 12 |  *   more details. | 
 | 13 |  * | 
 | 14 |  * TILE SMP support routines. | 
 | 15 |  */ | 
 | 16 |  | 
 | 17 | #include <linux/smp.h> | 
| Chris Metcalf | fb702b9 | 2010-06-25 16:41:11 -0400 | [diff] [blame] | 18 | #include <linux/interrupt.h> | 
 | 19 | #include <linux/io.h> | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 20 | #include <linux/irq.h> | 
| Chris Metcalf | fb702b9 | 2010-06-25 16:41:11 -0400 | [diff] [blame] | 21 | #include <linux/module.h> | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 22 | #include <asm/cacheflush.h> | 
 | 23 |  | 
 | 24 | HV_Topology smp_topology __write_once; | 
| Chris Metcalf | fb702b9 | 2010-06-25 16:41:11 -0400 | [diff] [blame] | 25 | EXPORT_SYMBOL(smp_topology); | 
 | 26 |  | 
 | 27 | #if CHIP_HAS_IPI() | 
 | 28 | static unsigned long __iomem *ipi_mappings[NR_CPUS]; | 
 | 29 | #endif | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 30 |  | 
 | 31 |  | 
 | 32 | /* | 
 | 33 |  * Top-level send_IPI*() functions to send messages to other cpus. | 
 | 34 |  */ | 
 | 35 |  | 
 | 36 | /* Set by smp_send_stop() to avoid recursive panics. */ | 
 | 37 | static int stopping_cpus; | 
 | 38 |  | 
| Chris Metcalf | bbeee4b2 | 2011-02-28 13:32:14 -0500 | [diff] [blame] | 39 | static void __send_IPI_many(HV_Recipient *recip, int nrecip, int tag) | 
 | 40 | { | 
 | 41 | 	int sent = 0; | 
 | 42 | 	while (sent < nrecip) { | 
 | 43 | 		int rc = hv_send_message(recip, nrecip, | 
 | 44 | 					 (HV_VirtAddr)&tag, sizeof(tag)); | 
 | 45 | 		if (rc < 0) { | 
 | 46 | 			if (!stopping_cpus)  /* avoid recursive panic */ | 
 | 47 | 				panic("hv_send_message returned %d", rc); | 
 | 48 | 			break; | 
 | 49 | 		} | 
 | 50 | 		WARN_ONCE(rc == 0, "hv_send_message() returned zero\n"); | 
 | 51 | 		sent += rc; | 
 | 52 | 	} | 
 | 53 | } | 
 | 54 |  | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 55 | void send_IPI_single(int cpu, int tag) | 
 | 56 | { | 
 | 57 | 	HV_Recipient recip = { | 
 | 58 | 		.y = cpu / smp_width, | 
 | 59 | 		.x = cpu % smp_width, | 
 | 60 | 		.state = HV_TO_BE_SENT | 
 | 61 | 	}; | 
| Chris Metcalf | bbeee4b2 | 2011-02-28 13:32:14 -0500 | [diff] [blame] | 62 | 	__send_IPI_many(&recip, 1, tag); | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 63 | } | 
 | 64 |  | 
 | 65 | void send_IPI_many(const struct cpumask *mask, int tag) | 
 | 66 | { | 
 | 67 | 	HV_Recipient recip[NR_CPUS]; | 
| Chris Metcalf | bbeee4b2 | 2011-02-28 13:32:14 -0500 | [diff] [blame] | 68 | 	int cpu; | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 69 | 	int nrecip = 0; | 
 | 70 | 	int my_cpu = smp_processor_id(); | 
 | 71 | 	for_each_cpu(cpu, mask) { | 
 | 72 | 		HV_Recipient *r; | 
 | 73 | 		BUG_ON(cpu == my_cpu); | 
 | 74 | 		r = &recip[nrecip++]; | 
 | 75 | 		r->y = cpu / smp_width; | 
 | 76 | 		r->x = cpu % smp_width; | 
 | 77 | 		r->state = HV_TO_BE_SENT; | 
 | 78 | 	} | 
| Chris Metcalf | bbeee4b2 | 2011-02-28 13:32:14 -0500 | [diff] [blame] | 79 | 	__send_IPI_many(recip, nrecip, tag); | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 80 | } | 
 | 81 |  | 
 | 82 | void send_IPI_allbutself(int tag) | 
 | 83 | { | 
 | 84 | 	struct cpumask mask; | 
 | 85 | 	cpumask_copy(&mask, cpu_online_mask); | 
 | 86 | 	cpumask_clear_cpu(smp_processor_id(), &mask); | 
 | 87 | 	send_IPI_many(&mask, tag); | 
 | 88 | } | 
 | 89 |  | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 90 | /* | 
 | 91 |  * Functions related to starting/stopping cpus. | 
 | 92 |  */ | 
 | 93 |  | 
 | 94 | /* Handler to start the current cpu. */ | 
 | 95 | static void smp_start_cpu_interrupt(void) | 
 | 96 | { | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 97 | 	get_irq_regs()->pc = start_cpu_function_addr; | 
 | 98 | } | 
 | 99 |  | 
 | 100 | /* Handler to stop the current cpu. */ | 
 | 101 | static void smp_stop_cpu_interrupt(void) | 
 | 102 | { | 
 | 103 | 	set_cpu_online(smp_processor_id(), 0); | 
| Chris Metcalf | 5d96611 | 2010-11-01 15:24:29 -0400 | [diff] [blame] | 104 | 	arch_local_irq_disable_all(); | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 105 | 	for (;;) | 
 | 106 | 		asm("nap"); | 
 | 107 | } | 
 | 108 |  | 
 | 109 | /* This function calls the 'stop' function on all other CPUs in the system. */ | 
 | 110 | void smp_send_stop(void) | 
 | 111 | { | 
 | 112 | 	stopping_cpus = 1; | 
 | 113 | 	send_IPI_allbutself(MSG_TAG_STOP_CPU); | 
 | 114 | } | 
 | 115 |  | 
 | 116 |  | 
 | 117 | /* | 
 | 118 |  * Dispatch code called from hv_message_intr() for HV_MSG_TILE hv messages. | 
 | 119 |  */ | 
 | 120 | void evaluate_message(int tag) | 
 | 121 | { | 
 | 122 | 	switch (tag) { | 
 | 123 | 	case MSG_TAG_START_CPU: /* Start up a cpu */ | 
 | 124 | 		smp_start_cpu_interrupt(); | 
 | 125 | 		break; | 
 | 126 |  | 
 | 127 | 	case MSG_TAG_STOP_CPU: /* Sent to shut down slave CPU's */ | 
 | 128 | 		smp_stop_cpu_interrupt(); | 
 | 129 | 		break; | 
 | 130 |  | 
 | 131 | 	case MSG_TAG_CALL_FUNCTION_MANY: /* Call function on cpumask */ | 
 | 132 | 		generic_smp_call_function_interrupt(); | 
 | 133 | 		break; | 
 | 134 |  | 
 | 135 | 	case MSG_TAG_CALL_FUNCTION_SINGLE: /* Call function on one other CPU */ | 
 | 136 | 		generic_smp_call_function_single_interrupt(); | 
 | 137 | 		break; | 
 | 138 |  | 
 | 139 | 	default: | 
 | 140 | 		panic("Unknown IPI message tag %d", tag); | 
 | 141 | 		break; | 
 | 142 | 	} | 
 | 143 | } | 
 | 144 |  | 
 | 145 |  | 
 | 146 | /* | 
 | 147 |  * flush_icache_range() code uses smp_call_function(). | 
 | 148 |  */ | 
 | 149 |  | 
 | 150 | struct ipi_flush { | 
 | 151 | 	unsigned long start; | 
 | 152 | 	unsigned long end; | 
 | 153 | }; | 
 | 154 |  | 
 | 155 | static void ipi_flush_icache_range(void *info) | 
 | 156 | { | 
 | 157 | 	struct ipi_flush *flush = (struct ipi_flush *) info; | 
 | 158 | 	__flush_icache_range(flush->start, flush->end); | 
 | 159 | } | 
 | 160 |  | 
 | 161 | void flush_icache_range(unsigned long start, unsigned long end) | 
 | 162 | { | 
 | 163 | 	struct ipi_flush flush = { start, end }; | 
 | 164 | 	preempt_disable(); | 
 | 165 | 	on_each_cpu(ipi_flush_icache_range, &flush, 1); | 
 | 166 | 	preempt_enable(); | 
 | 167 | } | 
 | 168 |  | 
 | 169 |  | 
| Chris Metcalf | fb702b9 | 2010-06-25 16:41:11 -0400 | [diff] [blame] | 170 | /* Called when smp_send_reschedule() triggers IRQ_RESCHEDULE. */ | 
 | 171 | static irqreturn_t handle_reschedule_ipi(int irq, void *token) | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 172 | { | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 173 | 	__get_cpu_var(irq_stat).irq_resched_count++; | 
| Peter Zijlstra | 184748c | 2011-04-05 17:23:39 +0200 | [diff] [blame] | 174 | 	scheduler_ipi(); | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 175 |  | 
 | 176 | 	return IRQ_HANDLED; | 
 | 177 | } | 
 | 178 |  | 
| Chris Metcalf | fb702b9 | 2010-06-25 16:41:11 -0400 | [diff] [blame] | 179 | static struct irqaction resched_action = { | 
 | 180 | 	.handler = handle_reschedule_ipi, | 
 | 181 | 	.name = "resched", | 
 | 182 | 	.dev_id = handle_reschedule_ipi /* unique token */, | 
 | 183 | }; | 
 | 184 |  | 
 | 185 | void __init ipi_init(void) | 
 | 186 | { | 
 | 187 | #if CHIP_HAS_IPI() | 
 | 188 | 	int cpu; | 
 | 189 | 	/* Map IPI trigger MMIO addresses. */ | 
 | 190 | 	for_each_possible_cpu(cpu) { | 
 | 191 | 		HV_Coord tile; | 
 | 192 | 		HV_PTE pte; | 
 | 193 | 		unsigned long offset; | 
 | 194 |  | 
 | 195 | 		tile.x = cpu_x(cpu); | 
 | 196 | 		tile.y = cpu_y(cpu); | 
| Chris Metcalf | a78c942 | 2010-10-14 16:23:03 -0400 | [diff] [blame] | 197 | 		if (hv_get_ipi_pte(tile, KERNEL_PL, &pte) != 0) | 
| Chris Metcalf | fb702b9 | 2010-06-25 16:41:11 -0400 | [diff] [blame] | 198 | 			panic("Failed to initialize IPI for cpu %d\n", cpu); | 
 | 199 |  | 
 | 200 | 		offset = hv_pte_get_pfn(pte) << PAGE_SHIFT; | 
 | 201 | 		ipi_mappings[cpu] = ioremap_prot(offset, PAGE_SIZE, pte); | 
 | 202 | 	} | 
 | 203 | #endif | 
 | 204 |  | 
 | 205 | 	/* Bind handle_reschedule_ipi() to IRQ_RESCHEDULE. */ | 
 | 206 | 	tile_irq_activate(IRQ_RESCHEDULE, TILE_IRQ_PERCPU); | 
 | 207 | 	BUG_ON(setup_irq(IRQ_RESCHEDULE, &resched_action)); | 
 | 208 | } | 
 | 209 |  | 
 | 210 | #if CHIP_HAS_IPI() | 
 | 211 |  | 
 | 212 | void smp_send_reschedule(int cpu) | 
 | 213 | { | 
 | 214 | 	WARN_ON(cpu_is_offline(cpu)); | 
 | 215 |  | 
 | 216 | 	/* | 
 | 217 | 	 * We just want to do an MMIO store.  The traditional writeq() | 
 | 218 | 	 * functions aren't really correct here, since they're always | 
 | 219 | 	 * directed at the PCI shim.  For now, just do a raw store, | 
 | 220 | 	 * casting away the __iomem attribute. | 
 | 221 | 	 */ | 
 | 222 | 	((unsigned long __force *)ipi_mappings[cpu])[IRQ_RESCHEDULE] = 0; | 
 | 223 | } | 
 | 224 |  | 
 | 225 | #else | 
 | 226 |  | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 227 | void smp_send_reschedule(int cpu) | 
 | 228 | { | 
 | 229 | 	HV_Coord coord; | 
 | 230 |  | 
 | 231 | 	WARN_ON(cpu_is_offline(cpu)); | 
| Chris Metcalf | fb702b9 | 2010-06-25 16:41:11 -0400 | [diff] [blame] | 232 |  | 
 | 233 | 	coord.y = cpu_y(cpu); | 
 | 234 | 	coord.x = cpu_x(cpu); | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 235 | 	hv_trigger_ipi(coord, IRQ_RESCHEDULE); | 
 | 236 | } | 
| Chris Metcalf | fb702b9 | 2010-06-25 16:41:11 -0400 | [diff] [blame] | 237 |  | 
 | 238 | #endif /* CHIP_HAS_IPI() */ |