blob: 2480433553e252bf76c79b7472a3478389177096 [file] [log] [blame]
Eric Holmbergf9cfa8e2011-09-23 14:29:11 -06001/* Copyright (c) 2008-2009, 2011, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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>
18
19#include <asm/system.h>
20
Eric Holmbergf9cfa8e2011-09-23 14:29:11 -060021#include <mach/msm_iomap.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <mach/remote_spinlock.h>
23#include <mach/dal.h>
24#include "smd_private.h"
25#include <linux/module.h>
26
Eric Holmbergf9cfa8e2011-09-23 14:29:11 -060027static void remote_spin_release_all_locks(uint32_t pid, int count);
28
29#if defined(CONFIG_MSM_REMOTE_SPINLOCK_SFPB)
30#define SFPB_SPINLOCK_COUNT 8
31#define MSM_SFPB_MUTEX_REG_BASE 0x01200600
32#define MSM_SFPB_MUTEX_REG_SIZE (33 * 4)
33
34static void *hw_mutex_reg_base;
35static DEFINE_MUTEX(hw_map_init_lock);
36
37static int remote_spinlock_init_address(int id, _remote_spinlock_t *lock)
38{
39 if (id >= SFPB_SPINLOCK_COUNT)
40 return -EINVAL;
41
42 if (!hw_mutex_reg_base) {
43 mutex_lock(&hw_map_init_lock);
44 if (!hw_mutex_reg_base)
45 hw_mutex_reg_base = ioremap(MSM_SFPB_MUTEX_REG_BASE,
46 MSM_SFPB_MUTEX_REG_SIZE);
47 mutex_unlock(&hw_map_init_lock);
48 BUG_ON(hw_mutex_reg_base == NULL);
49 }
50
51 *lock = hw_mutex_reg_base + 0x4 + id * 4;
52 return 0;
53}
54
55void _remote_spin_release_all(uint32_t pid)
56{
57 remote_spin_release_all_locks(pid, SFPB_SPINLOCK_COUNT);
58}
59
60#else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061#define SMEM_SPINLOCK_COUNT 8
62#define SMEM_SPINLOCK_ARRAY_SIZE (SMEM_SPINLOCK_COUNT * sizeof(uint32_t))
63
Eric Holmbergf9cfa8e2011-09-23 14:29:11 -060064static int remote_spinlock_init_address(int id, _remote_spinlock_t *lock)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065{
66 _remote_spinlock_t spinlock_start;
67
68 if (id >= SMEM_SPINLOCK_COUNT)
69 return -EINVAL;
70
71 spinlock_start = smem_alloc(SMEM_SPINLOCK_ARRAY,
72 SMEM_SPINLOCK_ARRAY_SIZE);
73 if (spinlock_start == NULL)
74 return -ENXIO;
75
76 *lock = spinlock_start + id;
77
78 return 0;
79}
80
Eric Holmbergf9cfa8e2011-09-23 14:29:11 -060081void _remote_spin_release_all(uint32_t pid)
82{
83 remote_spin_release_all_locks(pid, SMEM_SPINLOCK_COUNT);
84}
85
86#endif
87
88/**
89 * Release all spinlocks owned by @pid.
90 *
91 * This is only to be used for situations where the processor owning
92 * spinlocks has crashed and the spinlocks must be released.
93 *
94 * @pid - processor ID of processor to release
95 */
96static void remote_spin_release_all_locks(uint32_t pid, int count)
97{
98 int n;
99 _remote_spinlock_t lock;
100
101 for (n = 0; n < count; ++n) {
102 if (remote_spinlock_init_address(n, &lock) == 0)
103 _remote_spin_release(&lock, pid);
104 }
105}
106
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107static int
108remote_spinlock_dal_init(const char *chunk_name, _remote_spinlock_t *lock)
109{
110 void *dal_smem_start, *dal_smem_end;
111 uint32_t dal_smem_size;
112 struct dal_chunk_header *cur_header;
113
114 if (!chunk_name)
115 return -EINVAL;
116
117 dal_smem_start = smem_get_entry(SMEM_DAL_AREA, &dal_smem_size);
118 if (!dal_smem_start)
119 return -ENXIO;
120
121 dal_smem_end = dal_smem_start + dal_smem_size;
122
123 /* Find first chunk header */
124 cur_header = (struct dal_chunk_header *)
125 (((uint32_t)dal_smem_start + (4095)) & ~4095);
126 *lock = NULL;
127 while (cur_header->size != 0
128 && ((uint32_t)(cur_header + 1) < (uint32_t)dal_smem_end)) {
129
130 /* Check if chunk name matches */
131 if (!strncmp(cur_header->name, chunk_name,
132 DAL_CHUNK_NAME_LENGTH)) {
133 *lock = (_remote_spinlock_t)&cur_header->lock;
134 return 0;
135 }
136 cur_header = (void *)cur_header + cur_header->size;
137 }
138
139 pr_err("%s: DAL remote lock \"%s\" not found.\n", __func__,
140 chunk_name);
141 return -EINVAL;
142}
143
144int _remote_spin_lock_init(remote_spinlock_id_t id, _remote_spinlock_t *lock)
145{
146 BUG_ON(id == NULL);
147
148 if (id[0] == 'D' && id[1] == ':') {
149 /* DAL chunk name starts after "D:" */
150 return remote_spinlock_dal_init(&id[2], lock);
151 } else if (id[0] == 'S' && id[1] == ':') {
Eric Holmbergf9cfa8e2011-09-23 14:29:11 -0600152 /* Single-digit lock ID follows "S:" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153 BUG_ON(id[3] != '\0');
Eric Holmbergf9cfa8e2011-09-23 14:29:11 -0600154
155 return remote_spinlock_init_address((((uint8_t)id[2])-'0'),
156 lock);
157 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158 return -EINVAL;
Eric Holmbergf9cfa8e2011-09-23 14:29:11 -0600159 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160}
161
162int _remote_mutex_init(struct remote_mutex_id *id, _remote_mutex_t *lock)
163{
164 BUG_ON(id == NULL);
165
166 lock->delay_us = id->delay_us;
167 return _remote_spin_lock_init(id->r_spinlock_id, &(lock->r_spinlock));
168}
169EXPORT_SYMBOL(_remote_mutex_init);
170
171void _remote_mutex_lock(_remote_mutex_t *lock)
172{
173 while (!_remote_spin_trylock(&(lock->r_spinlock))) {
174 if (lock->delay_us >= 1000)
175 msleep(lock->delay_us/1000);
176 else
177 udelay(lock->delay_us);
178 }
179}
180EXPORT_SYMBOL(_remote_mutex_lock);
181
182void _remote_mutex_unlock(_remote_mutex_t *lock)
183{
184 _remote_spin_unlock(&(lock->r_spinlock));
185}
186EXPORT_SYMBOL(_remote_mutex_unlock);
187
188int _remote_mutex_trylock(_remote_mutex_t *lock)
189{
190 return _remote_spin_trylock(&(lock->r_spinlock));
191}
192EXPORT_SYMBOL(_remote_mutex_trylock);