summaryrefslogtreecommitdiffstats
path: root/lib/darwin.c
blob: df5032ecada7c8968570731e97735c0a63fa128c (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 *	The PCI Library -- Darwin kIOACPI access
 *
 *	Copyright (c) 2013 Apple, Inc.
 *
 *	Can be freely distributed and used under the terms of the GNU GPL v2+.
 *
 *	SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>

#include "internal.h"

#include <mach/mach_error.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOKitKeys.h>

enum {
    kACPIMethodAddressSpaceRead		= 0,
    kACPIMethodAddressSpaceWrite	= 1,
    kACPIMethodDebuggerCommand		= 2,
    kACPIMethodCount
};

#pragma pack(1)

typedef UInt32 IOACPIAddressSpaceID;

enum {
    kIOACPIAddressSpaceIDSystemMemory       = 0,
    kIOACPIAddressSpaceIDSystemIO           = 1,
    kIOACPIAddressSpaceIDPCIConfiguration   = 2,
    kIOACPIAddressSpaceIDEmbeddedController = 3,
    kIOACPIAddressSpaceIDSMBus              = 4
};

/*
 * 64-bit ACPI address
 */
union IOACPIAddress {
    UInt64 addr64;
    struct {
	unsigned int offset     :16;
	unsigned int function   :3;
	unsigned int device     :5;
	unsigned int bus        :8;
	unsigned int segment    :16;
	unsigned int reserved   :16;
    } pci;
};
typedef union IOACPIAddress IOACPIAddress;

#pragma pack()

struct AddressSpaceParam {
    UInt64			value;
    UInt32			spaceID;
    IOACPIAddress	address;
    UInt32			bitWidth;
    UInt32			bitOffset;
    UInt32			options;
};
typedef struct AddressSpaceParam AddressSpaceParam;

static void
darwin_config(struct pci_access *a UNUSED)
{
}

static int
darwin_detect(struct pci_access *a)
{
  io_registry_entry_t    service;
  io_connect_t           connect;
  kern_return_t          status;

  service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleACPIPlatformExpert"));
  if (service)
    {
      status = IOServiceOpen(service, mach_task_self(), 0, &connect);
      IOObjectRelease(service);
    }

  if (!service || (kIOReturnSuccess != status))
    {
      a->warning("Cannot open AppleACPIPlatformExpert (add boot arg debug=0x144 & run as root)");
      return 0;
    }
  a->debug("...using AppleACPIPlatformExpert");
  a->fd = connect;
  return 1;
}

static void
darwin_init(struct pci_access *a UNUSED)
{
}

static void
darwin_cleanup(struct pci_access *a UNUSED)
{
}

static int
darwin_read(struct pci_dev *d, int pos, byte *buf, int len)
{
  if (!(len == 1 || len == 2 || len == 4))
    return pci_generic_block_read(d, pos, buf, len);

  AddressSpaceParam param;
  kern_return_t     status;

  param.spaceID   = kIOACPIAddressSpaceIDPCIConfiguration;
  param.bitWidth  = len * 8;
  param.bitOffset = 0;
  param.options   = 0;

  param.address.pci.offset   = pos;
  param.address.pci.function = d->func;
  param.address.pci.device   = d->dev;
  param.address.pci.bus      = d->bus;
  param.address.pci.segment  = d->domain;
  param.address.pci.reserved = 0;
  param.value                = -1ULL;

  size_t outSize = sizeof(param);
  status = IOConnectCallStructMethod(d->access->fd, kACPIMethodAddressSpaceRead,
    &param, sizeof(param),
    &param, &outSize);
  if ((kIOReturnSuccess != status))
    d->access->error("darwin_read: kACPIMethodAddressSpaceRead failed: %s", mach_error_string(status));

  switch (len)
    {
    case 1:
      buf[0] = (u8) param.value;
      break;
    case 2:
      ((u16 *) buf)[0] = cpu_to_le16((u16) param.value);
      break;
    case 4:
      ((u32 *) buf)[0] = cpu_to_le32((u32) param.value);
      break;
    }
  return 1;
}

static int
darwin_write(struct pci_dev *d, int pos, byte *buf, int len)
{
  if (!(len == 1 || len == 2 || len == 4))
    return pci_generic_block_write(d, pos, buf, len);

  AddressSpaceParam param;
  kern_return_t     status;

  param.spaceID   = kIOACPIAddressSpaceIDPCIConfiguration;
  param.bitWidth  = len * 8;
  param.bitOffset = 0;
  param.options   = 0;

  param.address.pci.offset   = pos;
  param.address.pci.function = d->func;
  param.address.pci.device   = d->dev;
  param.address.pci.bus      = d->bus;
  param.address.pci.segment  = d->domain;
  param.address.pci.reserved = 0;

  switch (len)
    {
    case 1:
      param.value = buf[0];
      break;
    case 2:
      param.value = le16_to_cpu(((u16 *) buf)[0]);
      break;
    case 4:
      param.value = le32_to_cpu(((u32 *) buf)[0]);
      break;
    }

  size_t outSize = 0;
  status = IOConnectCallStructMethod(d->access->fd, kACPIMethodAddressSpaceWrite,
    &param, sizeof(param),
    NULL, &outSize);
  if ((kIOReturnSuccess != status))
    d->access->error("darwin_read: kACPIMethodAddressSpaceWrite failed: %s", mach_error_string(status));

  return 1;
}

struct pci_methods pm_darwin = {
    .name = "darwin",
    .help = "Darwin",
    .config = darwin_config,
    .detect = darwin_detect,
    .init = darwin_init,
    .cleanup = darwin_cleanup,
    .scan = pci_generic_scan,
    .fill_info = pci_generic_fill_info,
    .read = darwin_read,
    .write = darwin_write,
};