pci_insn.c 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * s390 specific pci instructions
 *
 * Copyright IBM Corp. 2013
 */

#include <linux/export.h>
#include <linux/errno.h>
#include <linux/delay.h>
10
#include <asm/facility.h>
11
#include <asm/pci_insn.h>
12
#include <asm/pci_debug.h>
13
#include <asm/processor.h>
14 15 16

#define ZPCI_INSN_BUSY_DELAY	1	/* 1 microsecond */

17 18 19 20 21
static inline void zpci_err_insn(u8 cc, u8 status, u64 req, u64 offset)
{
	struct {
		u64 req;
		u64 offset;
22 23 24
		u8 cc;
		u8 status;
	} __packed data = {req, offset, cc, status};
25 26 27 28

	zpci_err_hex(&data, sizeof(data));
}

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/* Modify PCI Function Controls */
static inline u8 __mpcifc(u64 req, struct zpci_fib *fib, u8 *status)
{
	u8 cc;

	asm volatile (
		"	.insn	rxy,0xe300000000d0,%[req],%[fib]\n"
		"	ipm	%[cc]\n"
		"	srl	%[cc],28\n"
		: [cc] "=d" (cc), [req] "+d" (req), [fib] "+Q" (*fib)
		: : "cc");
	*status = req >> 24 & 0xff;
	return cc;
}

44
u8 zpci_mod_fc(u64 req, struct zpci_fib *fib, u8 *status)
45
{
46
	u8 cc;
47 48

	do {
49
		cc = __mpcifc(req, fib, status);
50 51 52 53 54
		if (cc == 2)
			msleep(ZPCI_INSN_BUSY_DELAY);
	} while (cc == 2);

	if (cc)
55
		zpci_err_insn(cc, *status, req, 0);
56

57
	return cc;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
}

/* Refresh PCI Translations */
static inline u8 __rpcit(u64 fn, u64 addr, u64 range, u8 *status)
{
	register u64 __addr asm("2") = addr;
	register u64 __range asm("3") = range;
	u8 cc;

	asm volatile (
		"	.insn	rre,0xb9d30000,%[fn],%[addr]\n"
		"	ipm	%[cc]\n"
		"	srl	%[cc],28\n"
		: [cc] "=d" (cc), [fn] "+d" (fn)
		: [addr] "d" (__addr), "d" (__range)
		: "cc");
	*status = fn >> 24 & 0xff;
	return cc;
}

78
int zpci_refresh_trans(u64 fn, u64 addr, u64 range)
79 80 81 82 83 84 85 86 87 88
{
	u8 cc, status;

	do {
		cc = __rpcit(fn, addr, range, &status);
		if (cc == 2)
			udelay(ZPCI_INSN_BUSY_DELAY);
	} while (cc == 2);

	if (cc)
89 90
		zpci_err_insn(cc, status, addr, range);

91 92 93 94
	return (cc) ? -EIO : 0;
}

/* Set Interruption Controls */
95
int zpci_set_irq_ctrl(u16 ctl, char *unused, u8 isc)
96
{
97 98
	if (!test_facility(72))
		return -EIO;
99 100 101
	asm volatile (
		"	.insn	rsy,0xeb00000000d1,%[ctl],%[isc],%[u]\n"
		: : [ctl] "d" (ctl), [isc] "d" (isc << 27), [u] "Q" (*unused));
102
	return 0;
103 104 105
}

/* PCI Load */
106
static inline int ____pcilg(u64 *data, u64 req, u64 offset, u8 *status)
107 108 109
{
	register u64 __req asm("2") = req;
	register u64 __offset asm("3") = offset;
110
	int cc = -ENXIO;
111 112 113 114
	u64 __data;

	asm volatile (
		"	.insn	rre,0xb9d20000,%[data],%[req]\n"
115
		"0:	ipm	%[cc]\n"
116
		"	srl	%[cc],28\n"
117 118 119
		"1:\n"
		EX_TABLE(0b, 1b)
		: [cc] "+d" (cc), [data] "=d" (__data), [req] "+d" (__req)
120 121 122
		:  "d" (__offset)
		: "cc");
	*status = __req >> 24 & 0xff;
123 124 125 126 127 128 129 130 131 132
	*data = __data;
	return cc;
}

static inline int __pcilg(u64 *data, u64 req, u64 offset, u8 *status)
{
	u64 __data;
	int cc;

	cc = ____pcilg(&__data, req, offset, status);
133 134 135
	if (!cc)
		*data = __data;

136 137 138
	return cc;
}

139
int zpci_load(u64 *data, u64 req, u64 offset)
140
{
141 142
	u8 status;
	int cc;
143 144 145 146 147 148 149

	do {
		cc = __pcilg(data, req, offset, &status);
		if (cc == 2)
			udelay(ZPCI_INSN_BUSY_DELAY);
	} while (cc == 2);

150
	if (cc)
151 152
		zpci_err_insn(cc, status, req, offset);

153
	return (cc > 0) ? -EIO : cc;
154
}
155
EXPORT_SYMBOL_GPL(zpci_load);
156 157

/* PCI Store */
158
static inline int __pcistg(u64 data, u64 req, u64 offset, u8 *status)
159 160 161
{
	register u64 __req asm("2") = req;
	register u64 __offset asm("3") = offset;
162
	int cc = -ENXIO;
163 164 165

	asm volatile (
		"	.insn	rre,0xb9d00000,%[data],%[req]\n"
166
		"0:	ipm	%[cc]\n"
167
		"	srl	%[cc],28\n"
168 169 170
		"1:\n"
		EX_TABLE(0b, 1b)
		: [cc] "+d" (cc), [req] "+d" (__req)
171 172 173 174 175 176
		: "d" (__offset), [data] "d" (data)
		: "cc");
	*status = __req >> 24 & 0xff;
	return cc;
}

177
int zpci_store(u64 data, u64 req, u64 offset)
178
{
179 180
	u8 status;
	int cc;
181 182 183 184 185 186 187 188

	do {
		cc = __pcistg(data, req, offset, &status);
		if (cc == 2)
			udelay(ZPCI_INSN_BUSY_DELAY);
	} while (cc == 2);

	if (cc)
189 190
		zpci_err_insn(cc, status, req, offset);

191
	return (cc > 0) ? -EIO : cc;
192
}
193
EXPORT_SYMBOL_GPL(zpci_store);
194 195

/* PCI Store Block */
196
static inline int __pcistb(const u64 *data, u64 req, u64 offset, u8 *status)
197
{
198
	int cc = -ENXIO;
199 200 201

	asm volatile (
		"	.insn	rsy,0xeb00000000d0,%[req],%[offset],%[data]\n"
202
		"0:	ipm	%[cc]\n"
203
		"	srl	%[cc],28\n"
204 205 206
		"1:\n"
		EX_TABLE(0b, 1b)
		: [cc] "+d" (cc), [req] "+d" (req)
207 208 209 210 211 212
		: [offset] "d" (offset), [data] "Q" (*data)
		: "cc");
	*status = req >> 24 & 0xff;
	return cc;
}

213
int zpci_store_block(const u64 *data, u64 req, u64 offset)
214
{
215 216
	u8 status;
	int cc;
217 218 219 220 221 222 223 224

	do {
		cc = __pcistb(data, req, offset, &status);
		if (cc == 2)
			udelay(ZPCI_INSN_BUSY_DELAY);
	} while (cc == 2);

	if (cc)
225 226
		zpci_err_insn(cc, status, req, offset);

227
	return (cc > 0) ? -EIO : cc;
228
}
229
EXPORT_SYMBOL_GPL(zpci_store_block);