| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2008-2009, 2011-2013 The Linux Foundation. All rights reserved. | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 |  * | 
 | 3 |  * This program is free software; you can redistribute it and/or modify | 
 | 4 |  * it under the terms of the GNU General Public License version 2 and | 
 | 5 |  * only version 2 as published by the Free Software Foundation. | 
 | 6 |  * | 
 | 7 |  * This program is distributed in the hope that it will be useful, | 
 | 8 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 9 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 10 |  * GNU General Public License for more details. | 
 | 11 |  * | 
 | 12 |  */ | 
 | 13 |  | 
 | 14 | #include <linux/err.h> | 
 | 15 | #include <linux/kernel.h> | 
 | 16 | #include <linux/string.h> | 
 | 17 | #include <linux/delay.h> | 
| Jeff Hugo | 83e28f5 | 2012-12-12 15:16:37 -0700 | [diff] [blame] | 18 | #include <linux/module.h> | 
 | 19 | #include <linux/platform_device.h> | 
 | 20 | #include <linux/of.h> | 
 | 21 | #include <linux/of_address.h> | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 |  | 
 | 23 | #include <asm/system.h> | 
 | 24 |  | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 25 | #include <mach/msm_iomap.h> | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 26 | #include <mach/remote_spinlock.h> | 
 | 27 | #include <mach/dal.h> | 
 | 28 | #include "smd_private.h" | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 29 |  | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 30 |  | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 31 | #define SPINLOCK_PID_APPS 1 | 
 | 32 |  | 
 | 33 | #define AUTO_MODE -1 | 
 | 34 | #define DEKKERS_MODE 1 | 
 | 35 | #define SWP_MODE 2 | 
 | 36 | #define LDREX_MODE 3 | 
 | 37 | #define SFPB_MODE 4 | 
 | 38 |  | 
 | 39 | #if defined(CONFIG_MSM_REMOTE_SPINLOCK_DEKKERS) ||\ | 
 | 40 | 		defined(CONFIG_MSM_REMOTE_SPINLOCK_SWP) ||\ | 
 | 41 | 		defined(CONFIG_MSM_REMOTE_SPINLOCK_LDREX) ||\ | 
 | 42 | 		defined(CONFIG_MSM_REMOTE_SPINLOCK_SFPB) | 
 | 43 |  | 
 | 44 | #ifdef CONFIG_MSM_REMOTE_SPINLOCK_DEKKERS | 
 | 45 | /* | 
 | 46 |  * Use Dekker's algorithm when LDREX/STREX and SWP are unavailable for | 
 | 47 |  * shared memory | 
 | 48 |  */ | 
 | 49 | #define CURRENT_MODE_INIT DEKKERS_MODE; | 
 | 50 | #endif | 
 | 51 |  | 
 | 52 | #ifdef CONFIG_MSM_REMOTE_SPINLOCK_SWP | 
 | 53 | /* Use SWP-based locks when LDREX/STREX are unavailable for shared memory. */ | 
 | 54 | #define CURRENT_MODE_INIT SWP_MODE; | 
 | 55 | #endif | 
 | 56 |  | 
 | 57 | #ifdef CONFIG_MSM_REMOTE_SPINLOCK_LDREX | 
 | 58 | /* Use LDREX/STREX for shared memory locking, when available */ | 
 | 59 | #define CURRENT_MODE_INIT LDREX_MODE; | 
 | 60 | #endif | 
 | 61 |  | 
 | 62 | #ifdef CONFIG_MSM_REMOTE_SPINLOCK_SFPB | 
 | 63 | /* Use SFPB Hardware Mutex Registers */ | 
 | 64 | #define CURRENT_MODE_INIT SFPB_MODE; | 
 | 65 | #endif | 
 | 66 |  | 
 | 67 | #else | 
 | 68 | /* Use DT info to configure with a fallback to LDREX if DT is missing */ | 
 | 69 | #define CURRENT_MODE_INIT AUTO_MODE; | 
 | 70 | #endif | 
 | 71 |  | 
 | 72 | static int current_mode = CURRENT_MODE_INIT; | 
 | 73 |  | 
 | 74 | static int is_hw_lock_type; | 
 | 75 | static DEFINE_MUTEX(ops_init_lock); | 
 | 76 |  | 
 | 77 | struct spinlock_ops { | 
 | 78 | 	void (*lock)(raw_remote_spinlock_t *lock); | 
 | 79 | 	void (*unlock)(raw_remote_spinlock_t *lock); | 
 | 80 | 	int (*trylock)(raw_remote_spinlock_t *lock); | 
 | 81 | 	int (*release)(raw_remote_spinlock_t *lock, uint32_t pid); | 
 | 82 | 	int (*owner)(raw_remote_spinlock_t *lock); | 
 | 83 | }; | 
 | 84 |  | 
 | 85 | static struct spinlock_ops current_ops; | 
 | 86 |  | 
 | 87 | static int remote_spinlock_init_address(int id, _remote_spinlock_t *lock); | 
 | 88 |  | 
 | 89 | /* dekkers implementation --------------------------------------------------- */ | 
 | 90 | #define DEK_LOCK_REQUEST		1 | 
 | 91 | #define DEK_LOCK_YIELD			(!DEK_LOCK_REQUEST) | 
 | 92 | #define DEK_YIELD_TURN_SELF		0 | 
 | 93 | static void __raw_remote_dek_spin_lock(raw_remote_spinlock_t *lock) | 
 | 94 | { | 
 | 95 | 	lock->dek.self_lock = DEK_LOCK_REQUEST; | 
 | 96 |  | 
 | 97 | 	while (lock->dek.other_lock) { | 
 | 98 |  | 
 | 99 | 		if (lock->dek.next_yield == DEK_YIELD_TURN_SELF) | 
 | 100 | 			lock->dek.self_lock = DEK_LOCK_YIELD; | 
 | 101 |  | 
 | 102 | 		while (lock->dek.other_lock) | 
 | 103 | 			; | 
 | 104 |  | 
 | 105 | 		lock->dek.self_lock = DEK_LOCK_REQUEST; | 
 | 106 | 	} | 
 | 107 | 	lock->dek.next_yield = DEK_YIELD_TURN_SELF; | 
 | 108 |  | 
 | 109 | 	smp_mb(); | 
 | 110 | } | 
 | 111 |  | 
 | 112 | static int __raw_remote_dek_spin_trylock(raw_remote_spinlock_t *lock) | 
 | 113 | { | 
 | 114 | 	lock->dek.self_lock = DEK_LOCK_REQUEST; | 
 | 115 |  | 
 | 116 | 	if (lock->dek.other_lock) { | 
 | 117 | 		lock->dek.self_lock = DEK_LOCK_YIELD; | 
 | 118 | 		return 0; | 
 | 119 | 	} | 
 | 120 |  | 
 | 121 | 	lock->dek.next_yield = DEK_YIELD_TURN_SELF; | 
 | 122 |  | 
 | 123 | 	smp_mb(); | 
 | 124 | 	return 1; | 
 | 125 | } | 
 | 126 |  | 
 | 127 | static void __raw_remote_dek_spin_unlock(raw_remote_spinlock_t *lock) | 
 | 128 | { | 
 | 129 | 	smp_mb(); | 
 | 130 |  | 
 | 131 | 	lock->dek.self_lock = DEK_LOCK_YIELD; | 
 | 132 | } | 
 | 133 |  | 
 | 134 | static int __raw_remote_dek_spin_release(raw_remote_spinlock_t *lock, | 
 | 135 | 		uint32_t pid) | 
 | 136 | { | 
 | 137 | 	return -EPERM; | 
 | 138 | } | 
 | 139 |  | 
 | 140 | static int __raw_remote_dek_spin_owner(raw_remote_spinlock_t *lock) | 
 | 141 | { | 
 | 142 | 	return -EPERM; | 
 | 143 | } | 
 | 144 | /* end dekkers implementation ----------------------------------------------- */ | 
 | 145 |  | 
 | 146 | /* swp implementation ------------------------------------------------------- */ | 
 | 147 | static void __raw_remote_swp_spin_lock(raw_remote_spinlock_t *lock) | 
 | 148 | { | 
 | 149 | 	unsigned long tmp; | 
 | 150 |  | 
 | 151 | 	__asm__ __volatile__( | 
 | 152 | "1:     swp     %0, %2, [%1]\n" | 
 | 153 | "       teq     %0, #0\n" | 
 | 154 | "       bne     1b" | 
 | 155 | 	: "=&r" (tmp) | 
 | 156 | 	: "r" (&lock->lock), "r" (1) | 
 | 157 | 	: "cc"); | 
 | 158 |  | 
 | 159 | 	smp_mb(); | 
 | 160 | } | 
 | 161 |  | 
 | 162 | static int __raw_remote_swp_spin_trylock(raw_remote_spinlock_t *lock) | 
 | 163 | { | 
 | 164 | 	unsigned long tmp; | 
 | 165 |  | 
 | 166 | 	__asm__ __volatile__( | 
 | 167 | "       swp     %0, %2, [%1]\n" | 
 | 168 | 	: "=&r" (tmp) | 
 | 169 | 	: "r" (&lock->lock), "r" (1) | 
 | 170 | 	: "cc"); | 
 | 171 |  | 
 | 172 | 	if (tmp == 0) { | 
 | 173 | 		smp_mb(); | 
 | 174 | 		return 1; | 
 | 175 | 	} | 
 | 176 | 	return 0; | 
 | 177 | } | 
 | 178 |  | 
 | 179 | static void __raw_remote_swp_spin_unlock(raw_remote_spinlock_t *lock) | 
 | 180 | { | 
 | 181 | 	int lock_owner; | 
 | 182 |  | 
 | 183 | 	smp_mb(); | 
 | 184 | 	lock_owner = readl_relaxed(&lock->lock); | 
 | 185 | 	if (lock_owner != SPINLOCK_PID_APPS) { | 
 | 186 | 		pr_err("%s: spinlock not owned by Apps (actual owner is %d)\n", | 
 | 187 | 				__func__, lock_owner); | 
 | 188 | 	} | 
 | 189 |  | 
 | 190 | 	__asm__ __volatile__( | 
 | 191 | "       str     %1, [%0]" | 
 | 192 | 	: | 
 | 193 | 	: "r" (&lock->lock), "r" (0) | 
 | 194 | 	: "cc"); | 
 | 195 | } | 
 | 196 | /* end swp implementation --------------------------------------------------- */ | 
 | 197 |  | 
 | 198 | /* ldrex implementation ----------------------------------------------------- */ | 
 | 199 | static void __raw_remote_ex_spin_lock(raw_remote_spinlock_t *lock) | 
 | 200 | { | 
 | 201 | 	unsigned long tmp; | 
 | 202 |  | 
 | 203 | 	__asm__ __volatile__( | 
 | 204 | "1:     ldrex   %0, [%1]\n" | 
 | 205 | "       teq     %0, #0\n" | 
 | 206 | "       strexeq %0, %2, [%1]\n" | 
 | 207 | "       teqeq   %0, #0\n" | 
 | 208 | "       bne     1b" | 
 | 209 | 	: "=&r" (tmp) | 
 | 210 | 	: "r" (&lock->lock), "r" (SPINLOCK_PID_APPS) | 
 | 211 | 	: "cc"); | 
 | 212 |  | 
 | 213 | 	smp_mb(); | 
 | 214 | } | 
 | 215 |  | 
 | 216 | static int __raw_remote_ex_spin_trylock(raw_remote_spinlock_t *lock) | 
 | 217 | { | 
 | 218 | 	unsigned long tmp; | 
 | 219 |  | 
 | 220 | 	__asm__ __volatile__( | 
 | 221 | "       ldrex   %0, [%1]\n" | 
 | 222 | "       teq     %0, #0\n" | 
 | 223 | "       strexeq %0, %2, [%1]\n" | 
 | 224 | 	: "=&r" (tmp) | 
 | 225 | 	: "r" (&lock->lock), "r" (SPINLOCK_PID_APPS) | 
 | 226 | 	: "cc"); | 
 | 227 |  | 
 | 228 | 	if (tmp == 0) { | 
 | 229 | 		smp_mb(); | 
 | 230 | 		return 1; | 
 | 231 | 	} | 
 | 232 | 	return 0; | 
 | 233 | } | 
 | 234 |  | 
 | 235 | static void __raw_remote_ex_spin_unlock(raw_remote_spinlock_t *lock) | 
 | 236 | { | 
 | 237 | 	int lock_owner; | 
 | 238 |  | 
 | 239 | 	smp_mb(); | 
 | 240 | 	lock_owner = readl_relaxed(&lock->lock); | 
 | 241 | 	if (lock_owner != SPINLOCK_PID_APPS) { | 
 | 242 | 		pr_err("%s: spinlock not owned by Apps (actual owner is %d)\n", | 
 | 243 | 				__func__, lock_owner); | 
 | 244 | 	} | 
 | 245 |  | 
 | 246 | 	__asm__ __volatile__( | 
 | 247 | "       str     %1, [%0]\n" | 
 | 248 | 	: | 
 | 249 | 	: "r" (&lock->lock), "r" (0) | 
 | 250 | 	: "cc"); | 
 | 251 | } | 
 | 252 | /* end ldrex implementation ------------------------------------------------- */ | 
 | 253 |  | 
 | 254 | /* sfpb implementation ------------------------------------------------------ */ | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 255 | #define SFPB_SPINLOCK_COUNT 8 | 
 | 256 | #define MSM_SFPB_MUTEX_REG_BASE 0x01200600 | 
 | 257 | #define MSM_SFPB_MUTEX_REG_SIZE	(33 * 4) | 
| Jeff Hugo | 83e28f5 | 2012-12-12 15:16:37 -0700 | [diff] [blame] | 258 | #define SFPB_SPINLOCK_OFFSET 4 | 
 | 259 | #define SFPB_SPINLOCK_SIZE 4 | 
 | 260 |  | 
 | 261 | static uint32_t lock_count; | 
 | 262 | static phys_addr_t reg_base; | 
 | 263 | static uint32_t reg_size; | 
 | 264 | static uint32_t lock_offset; /* offset into the hardware block before lock 0 */ | 
 | 265 | static uint32_t lock_size; | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 266 |  | 
 | 267 | static void *hw_mutex_reg_base; | 
 | 268 | static DEFINE_MUTEX(hw_map_init_lock); | 
 | 269 |  | 
| Jeff Hugo | 83e28f5 | 2012-12-12 15:16:37 -0700 | [diff] [blame] | 270 | static char *compatible_string = "qcom,ipc-spinlock"; | 
 | 271 |  | 
 | 272 | static int init_hw_mutex(struct device_node *node) | 
 | 273 | { | 
 | 274 | 	struct resource r; | 
 | 275 | 	int rc; | 
 | 276 |  | 
 | 277 | 	rc = of_address_to_resource(node, 0, &r); | 
 | 278 | 	if (rc) | 
 | 279 | 		BUG(); | 
 | 280 |  | 
 | 281 | 	rc = of_property_read_u32(node, "qcom,num-locks", &lock_count); | 
 | 282 | 	if (rc) | 
 | 283 | 		BUG(); | 
 | 284 |  | 
 | 285 | 	reg_base = r.start; | 
 | 286 | 	reg_size = (uint32_t)(resource_size(&r)); | 
 | 287 | 	lock_offset = 0; | 
 | 288 | 	lock_size = reg_size / lock_count; | 
 | 289 |  | 
 | 290 | 	return 0; | 
 | 291 | } | 
 | 292 |  | 
 | 293 | static void find_and_init_hw_mutex(void) | 
 | 294 | { | 
 | 295 | 	struct device_node *node; | 
 | 296 |  | 
 | 297 | 	node = of_find_compatible_node(NULL, NULL, compatible_string); | 
 | 298 | 	if (node) { | 
 | 299 | 		init_hw_mutex(node); | 
 | 300 | 	} else { | 
 | 301 | 		lock_count = SFPB_SPINLOCK_COUNT; | 
 | 302 | 		reg_base = MSM_SFPB_MUTEX_REG_BASE; | 
 | 303 | 		reg_size = MSM_SFPB_MUTEX_REG_SIZE; | 
 | 304 | 		lock_offset = SFPB_SPINLOCK_OFFSET; | 
 | 305 | 		lock_size = SFPB_SPINLOCK_SIZE; | 
 | 306 | 	} | 
 | 307 | 	hw_mutex_reg_base = ioremap(reg_base, reg_size); | 
 | 308 | 	BUG_ON(hw_mutex_reg_base == NULL); | 
 | 309 | } | 
 | 310 |  | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 311 | static int remote_spinlock_init_address_hw(int id, _remote_spinlock_t *lock) | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 312 | { | 
| Jeff Hugo | 83e28f5 | 2012-12-12 15:16:37 -0700 | [diff] [blame] | 313 | 	/* | 
 | 314 | 	 * Optimistic locking.  Init only needs to be done once by the first | 
 | 315 | 	 * caller.  After that, serializing inits between different callers | 
 | 316 | 	 * is unnecessary.  The second check after the lock ensures init | 
 | 317 | 	 * wasn't previously completed by someone else before the lock could | 
 | 318 | 	 * be grabbed. | 
 | 319 | 	 */ | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 320 | 	if (!hw_mutex_reg_base) { | 
 | 321 | 		mutex_lock(&hw_map_init_lock); | 
 | 322 | 		if (!hw_mutex_reg_base) | 
| Jeff Hugo | 83e28f5 | 2012-12-12 15:16:37 -0700 | [diff] [blame] | 323 | 			find_and_init_hw_mutex(); | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 324 | 		mutex_unlock(&hw_map_init_lock); | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 325 | 	} | 
 | 326 |  | 
| Jeff Hugo | 83e28f5 | 2012-12-12 15:16:37 -0700 | [diff] [blame] | 327 | 	if (id >= lock_count) | 
 | 328 | 		return -EINVAL; | 
 | 329 |  | 
 | 330 | 	*lock = hw_mutex_reg_base + lock_offset + id * lock_size; | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 331 | 	return 0; | 
 | 332 | } | 
 | 333 |  | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 334 | static void __raw_remote_sfpb_spin_lock(raw_remote_spinlock_t *lock) | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 335 | { | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 336 | 	do { | 
 | 337 | 		writel_relaxed(SPINLOCK_PID_APPS, lock); | 
 | 338 | 		smp_mb(); | 
 | 339 | 	} while (readl_relaxed(lock) != SPINLOCK_PID_APPS); | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 340 | } | 
 | 341 |  | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 342 | static int __raw_remote_sfpb_spin_trylock(raw_remote_spinlock_t *lock) | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 343 | { | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 344 | 	return 1; | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 345 | } | 
 | 346 |  | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 347 | static void __raw_remote_sfpb_spin_unlock(raw_remote_spinlock_t *lock) | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 348 | { | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 349 | 	int lock_owner; | 
 | 350 |  | 
 | 351 | 	lock_owner = readl_relaxed(lock); | 
 | 352 | 	if (lock_owner != SPINLOCK_PID_APPS) { | 
 | 353 | 		pr_err("%s: spinlock not owned by Apps (actual owner is %d)\n", | 
 | 354 | 				__func__, lock_owner); | 
 | 355 | 	} | 
 | 356 |  | 
 | 357 | 	writel_relaxed(0, lock); | 
 | 358 | 	smp_mb(); | 
 | 359 | } | 
 | 360 | /* end sfpb implementation -------------------------------------------------- */ | 
 | 361 |  | 
 | 362 | /* common spinlock API ------------------------------------------------------ */ | 
 | 363 | /** | 
 | 364 |  * Release spinlock if it is owned by @pid. | 
 | 365 |  * | 
 | 366 |  * This is only to be used for situations where the processor owning | 
 | 367 |  * the spinlock has crashed and the spinlock must be released. | 
 | 368 |  * | 
 | 369 |  * @lock: lock structure | 
 | 370 |  * @pid: processor ID of processor to release | 
 | 371 |  */ | 
 | 372 | static int __raw_remote_gen_spin_release(raw_remote_spinlock_t *lock, | 
 | 373 | 		uint32_t pid) | 
 | 374 | { | 
 | 375 | 	int ret = 1; | 
 | 376 |  | 
 | 377 | 	if (readl_relaxed(&lock->lock) == pid) { | 
 | 378 | 		writel_relaxed(0, &lock->lock); | 
 | 379 | 		wmb(); | 
 | 380 | 		ret = 0; | 
 | 381 | 	} | 
 | 382 | 	return ret; | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 383 | } | 
 | 384 |  | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 385 | /** | 
 | 386 |  * Return owner of the spinlock. | 
 | 387 |  * | 
 | 388 |  * @lock: pointer to lock structure | 
 | 389 |  * @returns: >= 0 owned PID; < 0 for error case | 
 | 390 |  * | 
 | 391 |  * Used for testing.  PID's are assumed to be 31 bits or less. | 
 | 392 |  */ | 
 | 393 | static int __raw_remote_gen_spin_owner(raw_remote_spinlock_t *lock) | 
 | 394 | { | 
 | 395 | 	rmb(); | 
 | 396 | 	return readl_relaxed(&lock->lock); | 
 | 397 | } | 
 | 398 |  | 
 | 399 |  | 
 | 400 | static void initialize_ops(void) | 
 | 401 | { | 
 | 402 | 	struct device_node *node; | 
 | 403 |  | 
 | 404 | 	switch (current_mode) { | 
 | 405 | 	case DEKKERS_MODE: | 
 | 406 | 		current_ops.lock = __raw_remote_dek_spin_lock; | 
 | 407 | 		current_ops.unlock = __raw_remote_dek_spin_unlock; | 
 | 408 | 		current_ops.trylock = __raw_remote_dek_spin_trylock; | 
 | 409 | 		current_ops.release = __raw_remote_dek_spin_release; | 
 | 410 | 		current_ops.owner = __raw_remote_dek_spin_owner; | 
 | 411 | 		is_hw_lock_type = 0; | 
 | 412 | 		break; | 
 | 413 | 	case SWP_MODE: | 
 | 414 | 		current_ops.lock = __raw_remote_swp_spin_lock; | 
 | 415 | 		current_ops.unlock = __raw_remote_swp_spin_unlock; | 
 | 416 | 		current_ops.trylock = __raw_remote_swp_spin_trylock; | 
 | 417 | 		current_ops.release = __raw_remote_gen_spin_release; | 
 | 418 | 		current_ops.owner = __raw_remote_gen_spin_owner; | 
 | 419 | 		is_hw_lock_type = 0; | 
 | 420 | 		break; | 
 | 421 | 	case LDREX_MODE: | 
 | 422 | 		current_ops.lock = __raw_remote_ex_spin_lock; | 
 | 423 | 		current_ops.unlock = __raw_remote_ex_spin_unlock; | 
 | 424 | 		current_ops.trylock = __raw_remote_ex_spin_trylock; | 
 | 425 | 		current_ops.release = __raw_remote_gen_spin_release; | 
 | 426 | 		current_ops.owner = __raw_remote_gen_spin_owner; | 
 | 427 | 		is_hw_lock_type = 0; | 
 | 428 | 		break; | 
 | 429 | 	case SFPB_MODE: | 
 | 430 | 		current_ops.lock = __raw_remote_sfpb_spin_lock; | 
 | 431 | 		current_ops.unlock = __raw_remote_sfpb_spin_unlock; | 
 | 432 | 		current_ops.trylock = __raw_remote_sfpb_spin_trylock; | 
 | 433 | 		current_ops.release = __raw_remote_gen_spin_release; | 
 | 434 | 		current_ops.owner = __raw_remote_gen_spin_owner; | 
 | 435 | 		is_hw_lock_type = 1; | 
 | 436 | 		break; | 
 | 437 | 	case AUTO_MODE: | 
 | 438 | 		node = of_find_compatible_node(NULL, NULL, compatible_string); | 
 | 439 | 		if (node) { | 
 | 440 | 			current_ops.lock = __raw_remote_sfpb_spin_lock; | 
 | 441 | 			current_ops.unlock = __raw_remote_sfpb_spin_unlock; | 
 | 442 | 			current_ops.trylock = __raw_remote_sfpb_spin_trylock; | 
 | 443 | 			current_ops.release = __raw_remote_gen_spin_release; | 
 | 444 | 			current_ops.owner = __raw_remote_gen_spin_owner; | 
 | 445 | 			is_hw_lock_type = 1; | 
 | 446 | 		} else { | 
 | 447 | 			current_ops.lock = __raw_remote_ex_spin_lock; | 
 | 448 | 			current_ops.unlock = __raw_remote_ex_spin_unlock; | 
 | 449 | 			current_ops.trylock = __raw_remote_ex_spin_trylock; | 
 | 450 | 			current_ops.release = __raw_remote_gen_spin_release; | 
 | 451 | 			current_ops.owner = __raw_remote_gen_spin_owner; | 
 | 452 | 			is_hw_lock_type = 0; | 
 | 453 | 			pr_warn("Falling back to LDREX remote spinlock implementation"); | 
 | 454 | 		} | 
 | 455 | 		break; | 
 | 456 | 	default: | 
 | 457 | 		BUG(); | 
 | 458 | 		break; | 
 | 459 | 	} | 
 | 460 | } | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 461 |  | 
 | 462 | /** | 
 | 463 |  * Release all spinlocks owned by @pid. | 
 | 464 |  * | 
 | 465 |  * This is only to be used for situations where the processor owning | 
 | 466 |  * spinlocks has crashed and the spinlocks must be released. | 
 | 467 |  * | 
 | 468 |  * @pid - processor ID of processor to release | 
 | 469 |  */ | 
 | 470 | static void remote_spin_release_all_locks(uint32_t pid, int count) | 
 | 471 | { | 
 | 472 | 	int n; | 
 | 473 | 	 _remote_spinlock_t lock; | 
 | 474 |  | 
 | 475 | 	for (n = 0; n < count; ++n) { | 
 | 476 | 		if (remote_spinlock_init_address(n, &lock) == 0) | 
 | 477 | 			_remote_spin_release(&lock, pid); | 
 | 478 | 	} | 
 | 479 | } | 
 | 480 |  | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 481 | void _remote_spin_release_all(uint32_t pid) | 
 | 482 | { | 
 | 483 | 	remote_spin_release_all_locks(pid, lock_count); | 
 | 484 | } | 
 | 485 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 486 | static int | 
 | 487 | remote_spinlock_dal_init(const char *chunk_name, _remote_spinlock_t *lock) | 
 | 488 | { | 
 | 489 | 	void *dal_smem_start, *dal_smem_end; | 
 | 490 | 	uint32_t dal_smem_size; | 
 | 491 | 	struct dal_chunk_header *cur_header; | 
 | 492 |  | 
 | 493 | 	if (!chunk_name) | 
 | 494 | 		return -EINVAL; | 
 | 495 |  | 
 | 496 | 	dal_smem_start = smem_get_entry(SMEM_DAL_AREA, &dal_smem_size); | 
 | 497 | 	if (!dal_smem_start) | 
 | 498 | 		return -ENXIO; | 
 | 499 |  | 
 | 500 | 	dal_smem_end = dal_smem_start + dal_smem_size; | 
 | 501 |  | 
 | 502 | 	/* Find first chunk header */ | 
 | 503 | 	cur_header = (struct dal_chunk_header *) | 
 | 504 | 			(((uint32_t)dal_smem_start + (4095)) & ~4095); | 
 | 505 | 	*lock = NULL; | 
 | 506 | 	while (cur_header->size != 0 | 
 | 507 | 		&& ((uint32_t)(cur_header + 1) < (uint32_t)dal_smem_end)) { | 
 | 508 |  | 
 | 509 | 		/* Check if chunk name matches */ | 
 | 510 | 		if (!strncmp(cur_header->name, chunk_name, | 
 | 511 | 						DAL_CHUNK_NAME_LENGTH)) { | 
 | 512 | 			*lock = (_remote_spinlock_t)&cur_header->lock; | 
 | 513 | 			return 0; | 
 | 514 | 		} | 
 | 515 | 		cur_header = (void *)cur_header + cur_header->size; | 
 | 516 | 	} | 
 | 517 |  | 
 | 518 | 	pr_err("%s: DAL remote lock \"%s\" not found.\n", __func__, | 
 | 519 | 		chunk_name); | 
 | 520 | 	return -EINVAL; | 
 | 521 | } | 
 | 522 |  | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 523 | #define SMEM_SPINLOCK_COUNT 8 | 
 | 524 | #define SMEM_SPINLOCK_ARRAY_SIZE (SMEM_SPINLOCK_COUNT * sizeof(uint32_t)) | 
 | 525 |  | 
 | 526 | static int remote_spinlock_init_address_smem(int id, _remote_spinlock_t *lock) | 
 | 527 | { | 
 | 528 | 	_remote_spinlock_t spinlock_start; | 
 | 529 |  | 
 | 530 | 	if (id >= SMEM_SPINLOCK_COUNT) | 
 | 531 | 		return -EINVAL; | 
 | 532 |  | 
 | 533 | 	spinlock_start = smem_alloc(SMEM_SPINLOCK_ARRAY, | 
 | 534 | 				    SMEM_SPINLOCK_ARRAY_SIZE); | 
 | 535 | 	if (spinlock_start == NULL) | 
 | 536 | 		return -ENXIO; | 
 | 537 |  | 
 | 538 | 	*lock = spinlock_start + id; | 
 | 539 |  | 
 | 540 | 	lock_count = SMEM_SPINLOCK_COUNT; | 
 | 541 |  | 
 | 542 | 	return 0; | 
 | 543 | } | 
 | 544 |  | 
 | 545 | static int remote_spinlock_init_address(int id, _remote_spinlock_t *lock) | 
 | 546 | { | 
 | 547 | 	if (is_hw_lock_type) | 
 | 548 | 		return remote_spinlock_init_address_hw(id, lock); | 
 | 549 | 	else | 
 | 550 | 		return remote_spinlock_init_address_smem(id, lock); | 
 | 551 | } | 
 | 552 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 553 | int _remote_spin_lock_init(remote_spinlock_id_t id, _remote_spinlock_t *lock) | 
 | 554 | { | 
 | 555 | 	BUG_ON(id == NULL); | 
 | 556 |  | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 557 | 	/* | 
 | 558 | 	 * Optimistic locking.  Init only needs to be done once by the first | 
 | 559 | 	 * caller.  After that, serializing inits between different callers | 
 | 560 | 	 * is unnecessary.  The second check after the lock ensures init | 
 | 561 | 	 * wasn't previously completed by someone else before the lock could | 
 | 562 | 	 * be grabbed. | 
 | 563 | 	 */ | 
 | 564 | 	if (!current_ops.lock) { | 
 | 565 | 		mutex_lock(&ops_init_lock); | 
 | 566 | 		if (!current_ops.lock) | 
 | 567 | 			initialize_ops(); | 
 | 568 | 		mutex_unlock(&ops_init_lock); | 
 | 569 | 	} | 
 | 570 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 571 | 	if (id[0] == 'D' && id[1] == ':') { | 
 | 572 | 		/* DAL chunk name starts after "D:" */ | 
 | 573 | 		return remote_spinlock_dal_init(&id[2], lock); | 
 | 574 | 	} else if (id[0] == 'S' && id[1] == ':') { | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 575 | 		/* Single-digit lock ID follows "S:" */ | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 576 | 		BUG_ON(id[3] != '\0'); | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 577 |  | 
 | 578 | 		return remote_spinlock_init_address((((uint8_t)id[2])-'0'), | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 579 | 			lock); | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 580 | 	} else { | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 581 | 		return -EINVAL; | 
| Eric Holmberg | f9cfa8e | 2011-09-23 14:29:11 -0600 | [diff] [blame] | 582 | 	} | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 583 | } | 
 | 584 |  | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 585 | /* | 
 | 586 |  * lock comes in as a pointer to a pointer to the lock location, so it must | 
 | 587 |  * be dereferenced and casted to the right type for the actual lock | 
 | 588 |  * implementation functions | 
 | 589 |  */ | 
 | 590 | void _remote_spin_lock(_remote_spinlock_t *lock) | 
 | 591 | { | 
 | 592 | 	if (unlikely(!current_ops.lock)) | 
 | 593 | 		BUG(); | 
 | 594 | 	current_ops.lock((raw_remote_spinlock_t *)(*lock)); | 
 | 595 | } | 
 | 596 | EXPORT_SYMBOL(_remote_spin_lock); | 
 | 597 |  | 
 | 598 | void _remote_spin_unlock(_remote_spinlock_t *lock) | 
 | 599 | { | 
 | 600 | 	if (unlikely(!current_ops.unlock)) | 
 | 601 | 		BUG(); | 
 | 602 | 	current_ops.unlock((raw_remote_spinlock_t *)(*lock)); | 
 | 603 | } | 
 | 604 | EXPORT_SYMBOL(_remote_spin_unlock); | 
 | 605 |  | 
 | 606 | int _remote_spin_trylock(_remote_spinlock_t *lock) | 
 | 607 | { | 
 | 608 | 	if (unlikely(!current_ops.trylock)) | 
 | 609 | 		BUG(); | 
 | 610 | 	return current_ops.trylock((raw_remote_spinlock_t *)(*lock)); | 
 | 611 | } | 
 | 612 | EXPORT_SYMBOL(_remote_spin_trylock); | 
 | 613 |  | 
 | 614 | int _remote_spin_release(_remote_spinlock_t *lock, uint32_t pid) | 
 | 615 | { | 
 | 616 | 	if (unlikely(!current_ops.release)) | 
 | 617 | 		BUG(); | 
 | 618 | 	return current_ops.release((raw_remote_spinlock_t *)(*lock), pid); | 
 | 619 | } | 
 | 620 | EXPORT_SYMBOL(_remote_spin_release); | 
 | 621 |  | 
 | 622 | int _remote_spin_owner(_remote_spinlock_t *lock) | 
 | 623 | { | 
 | 624 | 	if (unlikely(!current_ops.owner)) | 
 | 625 | 		BUG(); | 
 | 626 | 	return current_ops.owner((raw_remote_spinlock_t *)(*lock)); | 
 | 627 | } | 
 | 628 | EXPORT_SYMBOL(_remote_spin_owner); | 
 | 629 | /* end common spinlock API -------------------------------------------------- */ | 
 | 630 |  | 
 | 631 | /* remote mutex implementation ---------------------------------------------- */ | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 632 | int _remote_mutex_init(struct remote_mutex_id *id, _remote_mutex_t *lock) | 
 | 633 | { | 
 | 634 | 	BUG_ON(id == NULL); | 
 | 635 |  | 
 | 636 | 	lock->delay_us = id->delay_us; | 
 | 637 | 	return _remote_spin_lock_init(id->r_spinlock_id, &(lock->r_spinlock)); | 
 | 638 | } | 
 | 639 | EXPORT_SYMBOL(_remote_mutex_init); | 
 | 640 |  | 
 | 641 | void _remote_mutex_lock(_remote_mutex_t *lock) | 
 | 642 | { | 
 | 643 | 	while (!_remote_spin_trylock(&(lock->r_spinlock))) { | 
 | 644 | 		if (lock->delay_us >= 1000) | 
 | 645 | 			msleep(lock->delay_us/1000); | 
 | 646 | 		else | 
 | 647 | 			udelay(lock->delay_us); | 
 | 648 | 	} | 
 | 649 | } | 
 | 650 | EXPORT_SYMBOL(_remote_mutex_lock); | 
 | 651 |  | 
 | 652 | void _remote_mutex_unlock(_remote_mutex_t *lock) | 
 | 653 | { | 
 | 654 | 	_remote_spin_unlock(&(lock->r_spinlock)); | 
 | 655 | } | 
 | 656 | EXPORT_SYMBOL(_remote_mutex_unlock); | 
 | 657 |  | 
 | 658 | int _remote_mutex_trylock(_remote_mutex_t *lock) | 
 | 659 | { | 
 | 660 | 	return _remote_spin_trylock(&(lock->r_spinlock)); | 
 | 661 | } | 
 | 662 | EXPORT_SYMBOL(_remote_mutex_trylock); | 
| Jeff Hugo | 416539f | 2013-01-16 17:24:36 -0700 | [diff] [blame] | 663 | /* end remote mutex implementation ------------------------------------------ */ |