aboutsummaryrefslogtreecommitdiff
path: root/sim/ppc/devices.h
blob: 5fad839387f856b119ba44355c5e76c3848cb853 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*  This file is part of the program psim.

    Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
    */


#ifndef _DEVICES_H_
#define _DEVICES_H_

#ifndef INLINE_DEVICES
#define INLINE_DEVICES
#endif

#ifndef STATIC_DEVICES
#define STATIC_DEVICES
#endif


/* forward declaration of types */
/* typedef struct _device device; -- in devices.h */


/* Address access attributes that can be attached to a devices address
   range */
typedef enum _access_type {
  access_invalid = 0,
  access_read = 1,
  access_write = 2,
  access_read_write = 3,
  access_exec = 4,
  access_read_exec = 5,
  access_write_exec = 6,
  access_read_write_exec = 7,
} access_type;

/* Address attachement types */
typedef enum _attach_type {
  attach_invalid,
  attach_callback,
  attach_default,
  attach_raw_memory,
} attach_type;


/* Operators on devices: */


/* Initialization:

   A device is made fully functional in two stages.

   1. It is created. A device is created _before_ it is entered into
   the device tree.  During creation any permenant structures needed
   by the device should be created/initialized.

   2. It is initialized.  Before a simulation run, each device in the
   device tree is initialized in prefix order.  As part of this
   initialization, a device should (re)attach its self to its parent
   as needed.

   */

typedef void (device_init_callback)
     (const device *me,
      psim *system);

#define DTRACE_INIT(OBJECT) \
     DTRACE(OBJECT, \
	    (#OBJECT "_init(me=0x%lx:%s system=0x%lx)\n", \
	     (long)me, me->full_name, (long)system))

/* Data transfers:

   A device may permit the reading/writing (IO) of its registers in
   one or more address spaces.  For instance, a PCI device may have
   config registers in its config space and control registers in both
   the io and memory spaces of a PCI bus.

   Similarly, a device may initiate a data transfer (DMA) by passing
   such a request up to its parent.

   Init:

   As part of its initialization (not creation) and possibly also as a
   consequence of IO a device may attach its self to one or more of
   the address spaces of its parent device.

   For instance, a PCI device, during initialization would attach its
   config registers (space=0?, base=0, nr_bytes=64) to its parent PCI
   bridge.  Later, due to a write to this config space, the same
   device may in turn find it necessary to also attach its self to
   it's parent's `memory' or `io' space.

   To perform these operations, a device will call upon its parent
   using either device_attach_address or device_detach_address.

   * Any address specified is according to what the device expects to
   see.

   * Any detach operation must exactly match a previous attach.

   * included with the attach or detach is the devices name, the
   parent may use this as part of determining how to map map between a
   child's address + space and its own.

   * at any time, at most one device can have a default mapping
   registered.


   IO:

   A device receives requests to perform reads/writes to its registers
   or memory either A. from a processor or B. from a parent device.

   The device may then in turn either A. resolve the IO request
   locally by processing the data or trigering an exception or
   B. re-mapping the access onto one of its local address spaces and
   then in turn passing that on to one of its children.

   * Any address passed is relative to the local device.  Eg for PCI
   config registers, the address would (normally) be in the range of 0
   to 63.

   * Any exception situtation triggered by an IO operation (processor
   != NULL) is handled in one of the following ways: 1. Machine check
   (and similar): issued immediatly by restarting the cpu; 2. External
   exception: issue delayed (using events.h) until the current
   instruction execution cycle is completed; 3. Slave device (and
   similar): the need for the interrupt is passed on to the devices
   parent (which being an interrupt control unit will in turn take one
   of the actions described here); 4. Forget it.

   * Any exception situtation trigered by a non IO operation
   (processor == NULL) is handled buy returning 0.

   * Transfers of size <= 8 and of a power of 2 *must* be correctly
   aligned and should be treated as a `single cycle' transfer.

   DMA:

   A device initiates a DMA transfer by calling its parent with the
   request.  At the top level (if not done earlier) this is reflected
   back down the tree as io read/writes to the target device.

   This function is subject to change ...

   */

typedef void (device_config_address_callback)
     (const device *me,
      const char *name,
      attach_type attach,
      int space,
      unsigned_word addr,
      unsigned nr_bytes,
      access_type access,
      const device *who); /*callback/default*/

#define DTRACE_ATTACH_ADDRESS(OBJECT) \
     DTRACE(OBJECT, \
	    (#OBJECT "_attach_address(me=0x%lx:%s, name=%s, attach=%ld, space=%ld, addr=0x%lx, nr_bytes=%ld, access=%ld, who=0x%lx)\n", \
	     (long)me, me->full_name, name, (long)attach, (long)space, \
	     (long)addr, (long)nr_bytes, (long)access, (long)who))
#define DTRACE_DETACH_ADDRESS(OBJECT) \
     DTRACE(OBJECT, \
	    (#OBJECT "_detach_address(me=0x%lx:%s, name=%s, attach=%ld, space=%ld, addr=0x%lx, nr_bytes=%ld, access=%ld, who=0x%lx)\n", \
	     (long)me, me->full_name, name, (long)attach, (long)space, \
	     (long)addr, (long)nr_bytes, (long)access, (long)who))


typedef unsigned (device_io_read_buffer_callback)
     (const device *me,
      void *dest,
      int space,
      unsigned_word addr,
      unsigned nr_bytes,
      cpu *processor,
      unsigned_word cia);

typedef unsigned (device_io_write_buffer_callback)
     (const device *me,
      const void *source,
      int space,
      unsigned_word addr,
      unsigned nr_bytes,
      cpu *processor,
      unsigned_word cia);

#define DTRACE_IO_READ_BUFFER(OBJECT) \
     DTRACE(OBJECT, \
	    (#OBJECT "_io_read_buffer(me=0x%lx:%s dest=0x%lx space=%ld addr=0x%lx nr_bytes=%ld processor=0x%lx cia=0x%lx)\n", \
	     (long)me, me->full_name, (long)dest, (long)space, (long)addr, \
	     (long)nr_bytes, (long)processor, (long)cia))
#define DTRACE_IO_WRITE_BUFFER(OBJECT) \
     DTRACE(OBJECT, \
	    (#OBJECT "_io_write_buffer(me=0x%lx:%s source=0x%lx space=%ld addr=0x%lx nr_bytes=%ld processor=0x%lx cia=0x%lx)\n", \
	     (long)me, me->full_name, (long)source, (long)space, (long)addr, \
	     (long)nr_bytes, (long)processor, (long)cia))


typedef unsigned (device_dma_read_buffer_callback)
     (const device *me,
      void *dest,
      int space,
      unsigned_word addr,
      unsigned nr_bytes);

typedef unsigned (device_dma_write_buffer_callback)
     (const device *me,
      const void *source,
      int space,
      unsigned_word addr,
      unsigned nr_bytes,
      int violate_read_only_section);

#define DTRACE_DMA_READ_BUFFER(OBJECT) \
     DTRACE(OBJECT, \
	    (#OBJECT "_dma_read_buffer(me=0x%lx:%s dest=0x%lx space=%ld addr=0x%lx nr_bytes=%ld)\n", \
	     (long)me, me->full_name, (long)dest, (long)space, (long)addr, (long)nr_bytes))
#define DTRACE_DMA_WRITE_BUFFER(OBJECT) \
     DTRACE(OBJECT, \
	    (#OBJECT "_dma_write_buffer(me=0x%lx:%s source=0x%lx space=%ld addr=0x%lx nr_bytes=%ld)\n", \
	     (long)me, me->full_name, (long)source, (long)space, (long)addr, (long)nr_bytes))


/* Interrupts:

   As mentioned above.  Instead of handling an interrupt directly, a
   device may instead pass the need to interrupt on to its parent.

   Init:

   Before passing interrupts up to is parent, a device must first
   attach its interrupt lines to the parent device.  To do this, the
   device uses the parents attach/detach calls.
  
   Interrupts:

   A child notifies a parent of a change in an interrupt lines status
   using the interrupt call.  Similarly, a parent may notify a child
   of any `interrupt ack' sequence using the interrupt_ack call.

   */

typedef void (device_config_interrupt_callback)
     (const device *me,
      const device *who,
      int interrupt_line,
      const char *name);

typedef void (device_interrupt_ack_callback)
     (const device *me,
      int interrupt_line,
      int interrupt_status);

typedef void (device_interrupt_callback)
     (const device *me,
      const device *who,
      int interrupt_line,
      int interrupt_status,
      cpu *processor,
      unsigned_word cia);


/* IOCTL:

   Very simply, a catch all for any thing that turns up that until now
   either hasn't been thought of or doesn't justify an extra function. */


typedef void (device_ioctl_callback)
     (const device *me,
      psim *system,
      cpu *processor,
      unsigned_word cia,
      ...);
      


/* the callbacks */

typedef struct _device_callbacks {
  /* initialization */
  device_init_callback *init;
  /* address/data config - from child */
  device_config_address_callback *attach_address;
  device_config_address_callback *detach_address;
  /* address/data transfer - to child */
  device_io_read_buffer_callback *io_read_buffer;
  device_io_write_buffer_callback *io_write_buffer;
  /* address/data transfer - from child */
  device_dma_read_buffer_callback *dma_read_buffer;
  device_dma_write_buffer_callback *dma_write_buffer;
  /* interrupt config - from child */
  device_config_interrupt_callback *attach_interrupt;
  device_config_interrupt_callback *detach_interrupt;
  /* interrupt transfer - from child */
  device_interrupt_callback *interrupt;
  /* interrupt transfer - to child */
  device_interrupt_ack_callback *interrupt_ack;
  /* back door to anything we've forgot */
  device_ioctl_callback *ioctl;
} device_callbacks;

/* A device */
struct _device {
  const char *name; /* eg rom@0x1234,0x400 */
  const char *full_name; /* eg /isa/rom@0x1234,0x400 */
  void *data; /* device specific data */
  const device_callbacks *callback;
  const device *parent;
};


/* Create a new device, finding it in the builtin device table */

INLINE_DEVICES const device *device_create
(const char *name,
 const char *full_name,
 const device *parent);

/* create a new device using the parameterized data */

INLINE_DEVICES const device *device_create_from
(const char *name,
 const char *full_name,
 void *data,
 const device_callbacks *callback,
 const device *parent);


/* Unimplemented call back functions.  These abort the simulation */

INLINE_DEVICES device_init_callback unimp_device_init;
INLINE_DEVICES device_config_address_callback unimp_device_attach_address;
INLINE_DEVICES device_config_address_callback unimp_device_detach_address;
INLINE_DEVICES device_io_read_buffer_callback unimp_device_io_read_buffer;
INLINE_DEVICES device_io_write_buffer_callback unimp_device_io_write_buffer;
INLINE_DEVICES device_dma_read_buffer_callback unimp_device_dma_read_buffer;
INLINE_DEVICES device_dma_write_buffer_callback unimp_device_dma_write_buffer;
INLINE_DEVICES device_config_interrupt_callback unimp_device_attach_interrupt;
INLINE_DEVICES device_config_interrupt_callback unimp_device_detach_interrupt;
INLINE_DEVICES device_interrupt_callback unimp_device_interrupt;
INLINE_DEVICES device_interrupt_ack_callback unimp_device_interrupt_ack;
STATIC_DEVICES device_ioctl_callback unimp_device_ioctl;

/* Pass through and ignore callback functions.  A call going towards
   the root device are passed on up, local calls are ignored and call
   downs abort */

INLINE_DEVICES device_init_callback ignore_device_init;
INLINE_DEVICES device_config_address_callback pass_device_attach_address;
INLINE_DEVICES device_config_address_callback pass_device_detach_address;
INLINE_DEVICES device_dma_read_buffer_callback pass_device_dma_read_buffer;
INLINE_DEVICES device_dma_write_buffer_callback pass_device_dma_write_buffer;
INLINE_DEVICES device_config_interrupt_callback pass_device_attach_interrupt;
INLINE_DEVICES device_config_interrupt_callback pass_device_detach_interrupt;
INLINE_DEVICES device_interrupt_callback pass_device_interrupt;

INLINE_DEVICES const device_callbacks *passthrough_device_callbacks
(void);



#endif /* _DEVICES_H_ */