2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/delay.h>
35 #include <linux/device.h>
36 #include <linux/jiffies.h>
37 #include <linux/types.h>
38 #include <linux/slab.h>
39 #include <linux/wait.h>
42 #include "nfp6000/nfp6000.h"
44 struct nfp_cpp_mutex {
48 unsigned long long address;
52 static u32 nfp_mutex_locked(u16 interface)
54 return (u32)interface << 16 | 0x000f;
57 static u32 nfp_mutex_unlocked(u16 interface)
59 return (u32)interface << 16 | 0x0000;
62 static u32 nfp_mutex_owner(u32 val)
67 static bool nfp_mutex_is_locked(u32 val)
69 return (val & 0xffff) == 0x000f;
72 static bool nfp_mutex_is_unlocked(u32 val)
74 return (val & 0xffff) == 0000;
77 /* If you need more than 65536 recursive locks, please rethink your code. */
78 #define NFP_MUTEX_DEPTH_MAX 0xffff
81 nfp_cpp_mutex_validate(u16 interface, int *target, unsigned long long address)
83 /* Not permitted on invalid interfaces */
84 if (NFP_CPP_INTERFACE_TYPE_of(interface) ==
85 NFP_CPP_INTERFACE_TYPE_INVALID)
88 /* Address must be 64-bit aligned */
92 if (*target != NFP_CPP_TARGET_MU)
99 * nfp_cpp_mutex_init() - Initialize a mutex location
100 * @cpp: NFP CPP handle
101 * @target: NFP CPP target ID (ie NFP_CPP_TARGET_CLS or NFP_CPP_TARGET_MU)
102 * @address: Offset into the address space of the NFP CPP target ID
103 * @key: Unique 32-bit value for this mutex
105 * The CPP target:address must point to a 64-bit aligned location, and
106 * will initialize 64 bits of data at the location.
108 * This creates the initial mutex state, as locked by this
109 * nfp_cpp_interface().
111 * This function should only be called when setting up
112 * the initial lock state upon boot-up of the system.
114 * Return: 0 on success, or -errno on failure
116 int nfp_cpp_mutex_init(struct nfp_cpp *cpp,
117 int target, unsigned long long address, u32 key)
119 const u32 muw = NFP_CPP_ID(target, 4, 0); /* atomic_write */
120 u16 interface = nfp_cpp_interface(cpp);
123 err = nfp_cpp_mutex_validate(interface, &target, address);
127 err = nfp_cpp_writel(cpp, muw, address + 4, key);
131 err = nfp_cpp_writel(cpp, muw, address, nfp_mutex_locked(interface));
139 * nfp_cpp_mutex_alloc() - Create a mutex handle
140 * @cpp: NFP CPP handle
141 * @target: NFP CPP target ID (ie NFP_CPP_TARGET_CLS or NFP_CPP_TARGET_MU)
142 * @address: Offset into the address space of the NFP CPP target ID
143 * @key: 32-bit unique key (must match the key at this location)
145 * The CPP target:address must point to a 64-bit aligned location, and
146 * reserve 64 bits of data at the location for use by the handle.
148 * Only target/address pairs that point to entities that support the
149 * MU Atomic Engine's CmpAndSwap32 command are supported.
151 * Return: A non-NULL struct nfp_cpp_mutex * on success, NULL on failure.
153 struct nfp_cpp_mutex *nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int target,
154 unsigned long long address, u32 key)
156 const u32 mur = NFP_CPP_ID(target, 3, 0); /* atomic_read */
157 u16 interface = nfp_cpp_interface(cpp);
158 struct nfp_cpp_mutex *mutex;
162 err = nfp_cpp_mutex_validate(interface, &target, address);
166 err = nfp_cpp_readl(cpp, mur, address + 4, &tmp);
173 mutex = kzalloc(sizeof(*mutex), GFP_KERNEL);
178 mutex->target = target;
179 mutex->address = address;
187 * nfp_cpp_mutex_free() - Free a mutex handle - does not alter the lock state
188 * @mutex: NFP CPP Mutex handle
190 void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex)
196 * nfp_cpp_mutex_lock() - Lock a mutex handle, using the NFP MU Atomic Engine
197 * @mutex: NFP CPP Mutex handle
199 * Return: 0 on success, or -errno on failure
201 int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex)
203 unsigned long warn_at = jiffies + NFP_MUTEX_WAIT_FIRST_WARN * HZ;
204 unsigned long err_at = jiffies + NFP_MUTEX_WAIT_ERROR * HZ;
205 unsigned int timeout_ms = 1;
208 /* We can't use a waitqueue here, because the unlocker
209 * might be on a separate CPU.
211 * So just wait for now.
214 err = nfp_cpp_mutex_trylock(mutex);
218 err = msleep_interruptible(timeout_ms);
221 "interrupted waiting for NFP mutex\n");
225 if (time_is_before_eq_jiffies(warn_at)) {
226 warn_at = jiffies + NFP_MUTEX_WAIT_NEXT_WARN * HZ;
228 "Warning: waiting for NFP mutex [depth:%hd target:%d addr:%llx key:%08x]\n",
230 mutex->target, mutex->address, mutex->key);
232 if (time_is_before_eq_jiffies(err_at)) {
233 nfp_err(mutex->cpp, "Error: mutex wait timed out\n");
242 * nfp_cpp_mutex_unlock() - Unlock a mutex handle, using the MU Atomic Engine
243 * @mutex: NFP CPP Mutex handle
245 * Return: 0 on success, or -errno on failure
247 int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex)
249 const u32 muw = NFP_CPP_ID(mutex->target, 4, 0); /* atomic_write */
250 const u32 mur = NFP_CPP_ID(mutex->target, 3, 0); /* atomic_read */
251 struct nfp_cpp *cpp = mutex->cpp;
256 interface = nfp_cpp_interface(cpp);
258 if (mutex->depth > 1) {
263 err = nfp_cpp_readl(mutex->cpp, mur, mutex->address + 4, &key);
267 if (key != mutex->key)
270 err = nfp_cpp_readl(mutex->cpp, mur, mutex->address, &value);
274 if (value != nfp_mutex_locked(interface))
277 err = nfp_cpp_writel(cpp, muw, mutex->address,
278 nfp_mutex_unlocked(interface));
287 * nfp_cpp_mutex_trylock() - Attempt to lock a mutex handle
288 * @mutex: NFP CPP Mutex handle
290 * Return: 0 if the lock succeeded, -errno on failure
292 int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex)
294 const u32 muw = NFP_CPP_ID(mutex->target, 4, 0); /* atomic_write */
295 const u32 mus = NFP_CPP_ID(mutex->target, 5, 3); /* test_set_imm */
296 const u32 mur = NFP_CPP_ID(mutex->target, 3, 0); /* atomic_read */
297 struct nfp_cpp *cpp = mutex->cpp;
301 if (mutex->depth > 0) {
302 if (mutex->depth == NFP_MUTEX_DEPTH_MAX)
308 /* Verify that the lock marker is not damaged */
309 err = nfp_cpp_readl(cpp, mur, mutex->address + 4, &key);
313 if (key != mutex->key)
316 /* Compare against the unlocked state, and if true,
317 * write the interface id into the top 16 bits, and
320 value = nfp_mutex_locked(nfp_cpp_interface(cpp));
322 /* We use test_set_imm here, as it implies a read
323 * of the current state, and sets the bits in the
324 * bytemask of the command to 1s. Since the mutex
325 * is guaranteed to be 64-bit aligned, the bytemask
326 * of this 32-bit command is ensured to be 8'b00001111,
327 * which implies that the lower 4 bits will be set to
328 * ones regardless of the initial state.
330 * Since this is a 'Readback' operation, with no Pull
331 * data, we can treat this as a normal Push (read)
332 * atomic, which returns the original value.
334 err = nfp_cpp_readl(cpp, mus, mutex->address, &tmp);
338 /* Was it unlocked? */
339 if (nfp_mutex_is_unlocked(tmp)) {
340 /* The read value can only be 0x....0000 in the unlocked state.
341 * If there was another contending for this lock, then
342 * the lock state would be 0x....000f
345 /* Write our owner ID into the lock
346 * While not strictly necessary, this helps with
347 * debug and bookkeeping.
349 err = nfp_cpp_writel(cpp, muw, mutex->address, value);
357 return nfp_mutex_is_locked(tmp) ? -EBUSY : -EINVAL;
361 * nfp_cpp_mutex_reclaim() - Unlock mutex if held by local endpoint
362 * @cpp: NFP CPP handle
363 * @target: NFP CPP target ID (ie NFP_CPP_TARGET_CLS or NFP_CPP_TARGET_MU)
364 * @address: Offset into the address space of the NFP CPP target ID
366 * Release lock if held by local system. Extreme care is advised, call only
367 * when no local lock users can exist.
369 * Return: 0 if the lock was OK, 1 if locked by us, -errno on invalid mutex
371 int nfp_cpp_mutex_reclaim(struct nfp_cpp *cpp, int target,
372 unsigned long long address)
374 const u32 mur = NFP_CPP_ID(target, 3, 0); /* atomic_read */
375 const u32 muw = NFP_CPP_ID(target, 4, 0); /* atomic_write */
376 u16 interface = nfp_cpp_interface(cpp);
380 err = nfp_cpp_mutex_validate(interface, &target, address);
385 err = nfp_cpp_readl(cpp, mur, address, &tmp);
389 if (nfp_mutex_is_unlocked(tmp) || nfp_mutex_owner(tmp) != interface)
393 err = nfp_cpp_writel(cpp, muw, address, nfp_mutex_unlocked(interface));