summaryrefslogtreecommitdiffstats
path: root/kernel/include/linux/lglock.h
blob: 6f035f635d0ec8364fbc078e57c54d87fe394b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * Specialised local-global spinlock. Can only be declared as global variables
 * to avoid overhead and keep things simple (and we don't want to start using
 * these inside dynamically allocated structures).
 *
 * "local/global locks" (lglocks) can be used to:
 *
 * - Provide fast exclusive access to per-CPU data, with exclusive access to
 *   another CPU's data allowed but possibly subject to contention, and to
 *   provide very slow exclusive access to all per-CPU data.
 * - Or to provide very fast and scalable read serialisation, and to provide
 *   very slow exclusive serialisation of data (not necessarily per-CPU data).
 *
 * Brlocks are also implemented as a short-hand notation for the latter use
 * case.
 *
 * Copyright 2009, 2010, Nick Piggin, Novell Inc.
 */
#ifndef __LINUX_LGLOCK_H
#define __LINUX_LGLOCK_H

#include <linux/spinlock.h>
#include <linux/lockdep.h>
#include <linux/percpu.h>
#include <linux/cpu.h>
#include <linux/notifier.h>

#ifdef CONFIG_SMP

#ifdef CONFIG_DEBUG_LOCK_ALLOC
#define LOCKDEP_INIT_MAP lockdep_init_map
#else
#define LOCKDEP_INIT_MAP(a, b, c, d)
#endif

struct lglock {
#ifdef CONFIG_PREEMPT_RT_FULL
	struct rt_mutex __percpu *lock;
#else
	arch_spinlock_t __percpu *lock;
#endif
#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lock_class_key lock_key;
	struct lockdep_map    lock_dep_map;
#endif
};

#ifdef CONFIG_PREEMPT_RT_FULL
# define DEFINE_LGLOCK(name)						\
	static DEFINE_PER_CPU(struct rt_mutex, name ## _lock)		\
	= __RT_MUTEX_INITIALIZER( name ## _lock);			\
	struct lglock name = { .lock = &name ## _lock }

# define DEFINE_STATIC_LGLOCK(name)					\
	static DEFINE_PER_CPU(struct rt_mutex, name ## _lock)		\
	= __RT_MUTEX_INITIALIZER( name ## _lock);			\
	static struct lglock name = { .lock = &name ## _lock }

#else

#define DEFINE_LGLOCK(name)						\
	static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock)		\
	= __ARCH_SPIN_LOCK_UNLOCKED;					\
	struct lglock name = { .lock = &name ## _lock }

#define DEFINE_STATIC_LGLOCK(name)					\
	static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock)		\
	= __ARCH_SPIN_LOCK_UNLOCKED;					\
	static struct lglock name = { .lock = &name ## _lock }
#endif

void lg_lock_init(struct lglock *lg, char *name);

void lg_local_lock(struct lglock *lg);
void lg_local_unlock(struct lglock *lg);
void lg_local_lock_cpu(struct lglock *lg, int cpu);
void lg_local_unlock_cpu(struct lglock *lg, int cpu);

void lg_double_lock(struct lglock *lg, int cpu1, int cpu2);
void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2);

void lg_global_lock(struct lglock *lg);
void lg_global_unlock(struct lglock *lg);

#ifndef CONFIG_PREEMPT_RT_FULL
#define lg_global_trylock_relax(name)	lg_global_lock(name)
#else
void lg_global_trylock_relax(struct lglock *lg);
#endif

#else
/* When !CONFIG_SMP, map lglock to spinlock */
#define lglock spinlock
#define DEFINE_LGLOCK(name) DEFINE_SPINLOCK(name)
#define DEFINE_STATIC_LGLOCK(name) static DEFINE_SPINLOCK(name)
#define lg_lock_init(lg, name) spin_lock_init(lg)
#define lg_local_lock spin_lock
#define lg_local_unlock spin_unlock
#define lg_local_lock_cpu(lg, cpu) spin_lock(lg)
#define lg_local_unlock_cpu(lg, cpu) spin_unlock(lg)
#define lg_global_lock spin_lock
#define lg_global_unlock spin_unlock
#endif

#endif
ct cpm_param { u16 rbase; u16 tbase; u8 rfcr; u8 tfcr; u16 mrblr; u32 rstate; u8 res1[4]; u16 rbptr; u8 res2[6]; u32 tstate; u8 res3[4]; u16 tbptr; u8 res4[6]; u16 maxidl; u16 idlc; u16 brkln; u16 brkec; u16 brkcr; u16 rmask; u8 res5[4]; }; struct cpm_bd { u16 sc; /* Status and Control */ u16 len; /* Data length in buffer */ u8 *addr; /* Buffer address in host memory */ }; static void *cpcr; static struct cpm_param *param; static struct cpm_smc *smc; static struct cpm_scc *scc; static struct cpm_bd *tbdf, *rbdf; static u32 cpm_cmd; static void *cbd_addr; static u32 cbd_offset; static void (*do_cmd)(int op); static void (*enable_port)(void); static void (*disable_port)(void); #define CPM_CMD_STOP_TX 4 #define CPM_CMD_RESTART_TX 6 #define CPM_CMD_INIT_RX_TX 0 static void cpm1_cmd(int op) { while (in_be16(cpcr) & 1) ; out_be16(cpcr, (op << 8) | cpm_cmd | 1); while (in_be16(cpcr) & 1) ; } static void cpm2_cmd(int op) { while (in_be32(cpcr) & 0x10000) ; out_be32(cpcr, op | cpm_cmd | 0x10000); while (in_be32(cpcr) & 0x10000) ; } static void smc_disable_port(void) { do_cmd(CPM_CMD_STOP_TX); out_be16(&smc->smcmr, in_be16(&smc->smcmr) & ~3); } static void scc_disable_port(void) { do_cmd(CPM_CMD_STOP_TX); out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) & ~0x30); } static void smc_enable_port(void) { out_be16(&smc->smcmr, in_be16(&smc->smcmr) | 3); do_cmd(CPM_CMD_RESTART_TX); } static void scc_enable_port(void) { out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) | 0x30); do_cmd(CPM_CMD_RESTART_TX); } static int cpm_serial_open(void) { disable_port(); out_8(&param->rfcr, 0x10); out_8(&param->tfcr, 0x10); out_be16(&param->mrblr, 1); out_be16(&param->maxidl, 0); out_be16(&param->brkec, 0); out_be16(&param->brkln, 0); out_be16(&param->brkcr, 0); rbdf = cbd_addr; rbdf->addr = (u8 *)rbdf - 1; rbdf->sc = 0xa000; rbdf->len = 1; tbdf = rbdf + 1; tbdf->addr = (u8 *)rbdf - 2; tbdf->sc = 0x2000; tbdf->len = 1; sync(); out_be16(&param->rbase, cbd_offset); out_be16(&param->tbase, cbd_offset + sizeof(struct cpm_bd)); do_cmd(CPM_CMD_INIT_RX_TX); enable_port(); return 0; } static void cpm_serial_putc(unsigned char c) { while (tbdf->sc & 0x8000) barrier(); sync(); tbdf->addr[0] = c; eieio(); tbdf->sc |= 0x8000; } static unsigned char cpm_serial_tstc(void) { barrier(); return !(rbdf->sc & 0x8000); } static unsigned char cpm_serial_getc(void) { unsigned char c; while (!cpm_serial_tstc()) ; sync(); c = rbdf->addr[0]; eieio(); rbdf->sc |= 0x8000; return c; } int cpm_console_init(void *devp, struct serial_console_data *scdp) { void *vreg[2]; u32 reg[2]; int is_smc = 0, is_cpm2 = 0; void *parent, *muram; void *muram_addr; unsigned long muram_offset, muram_size; if (dt_is_compatible(devp, "fsl,cpm1-smc-uart")) { is_smc = 1; } else if (dt_is_compatible(devp, "fsl,cpm2-scc-uart")) { is_cpm2 = 1; } else if (dt_is_compatible(devp, "fsl,cpm2-smc-uart")) { is_cpm2 = 1; is_smc = 1; } if (is_smc) { enable_port = smc_enable_port; disable_port = smc_disable_port; } else { enable_port = scc_enable_port; disable_port = scc_disable_port; } if (is_cpm2) do_cmd = cpm2_cmd; else do_cmd = cpm1_cmd; if (getprop(devp, "fsl,cpm-command", &cpm_cmd, 4) < 4) return -1; if (dt_get_virtual_reg(devp, vreg, 2) < 2) return -1; if (is_smc) smc = vreg[0]; else scc = vreg[0]; param = vreg[1]; parent = get_parent(devp); if (!parent) return -1; if (dt_get_virtual_reg(parent, &cpcr, 1) < 1) return -1; muram = finddevice("/soc/cpm/muram/data"); if (!muram) return -1; /* For bootwrapper-compatible device trees, we assume that the first * entry has at least 128 bytes, and that #address-cells/#data-cells * is one for both parent and child. */ if (dt_get_virtual_reg(muram, &muram_addr, 1) < 1) return -1; if (getprop(muram, "reg", reg, 8) < 8) return -1; muram_offset = reg[0]; muram_size = reg[1]; /* Store the buffer descriptors at the end of the first muram chunk. * For SMC ports on CPM2-based platforms, relocate the parameter RAM * just before the buffer descriptors. */ cbd_offset = muram_offset + muram_size - 2 * sizeof(struct cpm_bd); if (is_cpm2 && is_smc) { u16 *smc_base = (u16 *)param; u16 pram_offset; pram_offset = cbd_offset - 64; pram_offset = _ALIGN_DOWN(pram_offset, 64); disable_port(); out_be16(smc_base, pram_offset); param = muram_addr - muram_offset + pram_offset; } cbd_addr = muram_addr - muram_offset + cbd_offset; scdp->open = cpm_serial_open; scdp->putc = cpm_serial_putc; scdp->getc = cpm_serial_getc; scdp->tstc = cpm_serial_tstc; return 0; }