summaryrefslogtreecommitdiffstats
path: root/libpdbg/host.c
blob: e799b964331782b9d3a97c7a46b048d9fd533e5c (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Copyright 2016 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE
#endif
#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <err.h>
#include <inttypes.h>

#include "bitutils.h"
#include "operations.h"
#include "target.h"

#define XSCOM_BASE_PATH "/sys/kernel/debug/powerpc/scom"

static uint64_t xscom_mangle_addr(uint64_t addr)
{
	uint64_t tmp;

	/*
	 * Shift the top 4 bits (indirect mode) down by 4 bits so we
	 * don't lose going through the debugfs interfaces.
	 */
	tmp = (addr & 0xf000000000000000) >> 4;
	addr &= 0x00ffffffffffffff;
	addr |= tmp;

	/* Shift up by 3 for debugfs */
	return addr << 3;
}

static int xscom_read(struct pib *pib, uint64_t addr, uint64_t *val)
{
	int rc;
	int fd = *(int *) pib->priv;

	addr = xscom_mangle_addr(addr);
	lseek64(fd, addr, SEEK_SET);
	rc = read(fd, val, 8);
	if (rc != 8)
		return -1;

	return 0;
}

static int xscom_write(struct pib *pib, uint64_t addr, uint64_t val)
{
	int rc;
	int fd = *(int *) pib->priv;

	addr = xscom_mangle_addr(addr);
	lseek64(fd, addr, SEEK_SET);
	rc = write(fd, &val, 8);
	if (rc != 8)
		return -1;

	return 0;
}

static int host_pib_probe(struct target *target)
{
	struct pib *pib = target_to_pib(target);
	int *fd;
	char *access_fn;
	uint32_t chip_id;

	fd = malloc(sizeof(fd));
	if (!fd)
		return -1;

	chip_id = dt_prop_get_u32(target->dn, "chip-id");
	if (asprintf(&access_fn, "%s/%08d/access", XSCOM_BASE_PATH, chip_id) < 0) {
		free(fd);
		return -1;
	}

	*fd = open(access_fn, O_RDWR);
	free(access_fn);
	if (fd < 0)
		return -1;

	pib->priv = fd;

	return 0;
}

struct pib host_pib = {
	.target = {
		.name = "Host based debugfs SCOM",
		.compatible  = "ibm,host-pib",
		.class = "pib",
		.probe = host_pib_probe,
	},
	.read = xscom_read,
	.write = xscom_write,
};
DECLARE_HW_UNIT(host_pib);
OpenPOWER on IntegriCloud