aboutsummaryrefslogtreecommitdiff
path: root/sim/ppc/hw_opic.c
blob: c314347e5a52f04e9e78c4c6b8f428075b2aade2 (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
/*  This file is part of the program psim.

    Copyright (C) 1994-1996, 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 _HW_OPIC_C_
#define _HW_OPIC_C_

#include "device_table.h"

#ifdef HAVE_STRING_H
#include <string.h>
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif


/* DEVICE


   opic - Open Programmable Interrupt Controller (OpenPIC)


   DESCRIPTION


   This device implements the core of the OpenPIC interrupt controller
   as described in the OpenPIC specification 1.2 and other related
   documents.

   The model includes:

   o	Up to 2048 external interrupt sources

   o	The four count down timers

   o	The four interprocessor multicast interrupts

   o	multiprocessor support

   o	Full tracing to assist help debugging

   o	Support for all variations of edge/level x high/low polarity.



   PROPERTIES


   reg = <address> <size> ... (required)

   Determine where the device lives in the parents address space.  The
   first <<address>> <<size>> pair specifies the address of the
   interrupt destination unit (which might contain an interrupt source
   unit) while successive reg entries specify additional interrupt
   source units.

   Note that for an <<opic>> device attached to a <<pci>> bus, the
   first <<reg>> entry may need to be ignored it will be the address
   of the devices configuration registers.


   interrupt-ranges = <int-number> <range> ... (required)

   A list of pairs.  Each pair corresponds to a block of interrupt
   source units (the address of which being specified by the
   corresponding reg tupple).  <<int-number>> is the number of the
   first interrupt in the block while <<range>> is the number of
   interrupts in the block.


   timer-frequency = <integer>  (optional)

   If present, specifies the default value of the timer frequency
   reporting register.  By default a value of 1 HZ is used.  The value
   is arbitrary, the timers are always updated once per machine cycle.


   vendor-identification = <integer>  (optional)

   If present, specifies the value to be returned when the vendor
   identification register is read.


   EXAMPLES


   See the test suite directory:

   |  psim-test/hw-opic


   BUGS

   For an OPIC controller attached to a PCI bus, it is not clear what
   the value of the <<reg>> and <<interrupt-ranges>> properties should
   be.  In particular, the PCI firmware bindings require the first
   value of the <<reg>> property to specify the devices configuration
   address while the OpenPIC bindings require that same entry to
   specify the address of the Interrupt Delivery Unit.  This
   implementation checks for and, if present, ignores any
   configuration address (and its corresponding <<interrupt-ranges>>
   entry).

   The OpenPIC specification requires the controller to be fair when
   distributing interrupts between processors.  At present the
   algorithm used isn't fair.  It is biased towards processor zero.

   The OpenPIC specification includes a 8259 pass through mode.  This
   is not supported.


   REFERENCES

   
   PowerPC Multiprocessor Interrupt Controller (MPIC), January 19,
   1996. Available from IBM.


   The Open Programmable Interrupt Controller (PIC) Register Interface
   Specification Revision 1.2.  Issue Date: Opctober 1995.  Available
   somewhere on AMD's web page (http://www.amd.com/)


   PowerPC Microprocessor Common Hardware Reference Platform (CHRP)
   System bindings to: IEEE Std 1275-1994 Standard for Boot
   (Initialization, Configuration) Firmware.  Revision 1.2b (INTERIM
   DRAFT).  April 22, 1996.  Available on the Open Firmware web site
   http://playground.sun.com/p1275/.


   */


/* forward types */

typedef struct _hw_opic_device hw_opic_device;


/* bounds */

enum {
  max_nr_interrupt_sources = 2048,
  max_nr_interrupt_destinations = 32,
  max_nr_task_priorities = 16,
};


enum {
  opic_alignment = 16,
};


/* global configuration register */

enum {
  gcr0_8259_bit = 0x20000000,
  gcr0_reset_bit = 0x80000000,
};


/* offsets and sizes */

enum {
  idu_isu_base = 0x10000,
  sizeof_isu_register_block = 32,
  idu_per_processor_register_base = 0x20000,
  sizeof_idu_per_processor_register_block = 0x1000,
  idu_timer_base = 0x01100,
  sizeof_timer_register_block = 0x00040,
};


/* Interrupt sources */

enum {
  isu_mask_bit = 0x80000000,
  isu_active_bit = 0x40000000,
  isu_multicast_bit = 0x20000000,
  isu_positive_polarity_bit = 0x00800000,
  isu_level_triggered_bit = 0x00400000,
  isu_priority_shift = 16,
  isu_vector_bits = 0x000000ff,
};


typedef struct _opic_interrupt_source {
  unsigned is_masked; /* left in place */
  unsigned is_multicast; /* left in place */
  unsigned is_positive_polarity; /* left in place */
  unsigned is_level_triggered; /* left in place */
  unsigned priority;
  unsigned vector;
  /* misc */
  int nr;
  unsigned destination;
  unsigned pending;
  unsigned in_service;
} opic_interrupt_source;


/* interrupt destinations (normally processors) */

typedef struct _opic_interrupt_destination {
  int nr;
  unsigned base_priority;
  opic_interrupt_source *current_pending;
  opic_interrupt_source *current_in_service;
  unsigned bit;
  int init_port;
  int intr_port;
} opic_interrupt_destination;


/* address map descriptors */

typedef struct _opic_isu_block { /* interrupt source unit block */
  int space;
  unsigned_word address;
  unsigned size;
  unsigned_cell int_number;
  unsigned_cell range;
  int reg;
} opic_isu_block;


typedef struct _opic_idu { /* interrupt delivery unit */
  int reg;
  int space;
  unsigned_word address;
  unsigned size;
} opic_idu;

typedef enum {
  /* bad */
  invalid_opic_register,
  /* interrupt source */
  interrupt_source_N_destination_register,
  interrupt_source_N_vector_priority_register,
  /* timers */
  timer_N_destination_register,
  timer_N_vector_priority_register,
  timer_N_base_count_register,
  timer_N_current_count_register,
  timer_frequency_reporting_register,
  /* inter-processor interrupts */
  ipi_N_vector_priority_register,
  ipi_N_dispatch_register,
  /* global configuration */
  spurious_vector_register,
  processor_init_register,
  vendor_identification_register,
  global_configuration_register_N,
  feature_reporting_register_N,
  /* per processor */
  end_of_interrupt_register_N,
  interrupt_acknowledge_register_N,
  current_task_priority_register_N,
} opic_register;

static const char *
opic_register_name(opic_register type)
{
  switch (type) {
  case invalid_opic_register: return "invalid_opic_register";
  case interrupt_source_N_destination_register: return "interrupt_source_N_destination_register";
  case interrupt_source_N_vector_priority_register: return "interrupt_source_N_vector_priority_register";
  case timer_N_destination_register: return "timer_N_destination_register";
  case timer_N_vector_priority_register: return "timer_N_vector_priority_register";
  case timer_N_base_count_register: return "timer_N_base_count_register";
  case timer_N_current_count_register: return "timer_N_current_count_register";
  case timer_frequency_reporting_register: return "timer_frequency_reporting_register";
  case ipi_N_vector_priority_register: return "ipi_N_vector_priority_register";
  case ipi_N_dispatch_register: return "ipi_N_dispatch_register";
  case spurious_vector_register: return "spurious_vector_register";
  case processor_init_register: return "processor_init_register";
  case vendor_identification_register: return "vendor_identification_register";
  case global_configuration_register_N: return "global_configuration_register_N";
  case feature_reporting_register_N: return "feature_reporting_register_N";
  case end_of_interrupt_register_N: return "end_of_interrupt_register_N";
  case interrupt_acknowledge_register_N: return "interrupt_acknowledge_register_N";
  case current_task_priority_register_N: return "current_task_priority_register_N";
  }
  return NULL;
}



/* timers */

typedef struct _opic_timer {
  int nr;
  device *me; /* find my way home */
  hw_opic_device *opic; /* ditto */
  unsigned base_count;
  int inhibited;
  signed64 count; /* *ONLY* if inhibited */
  event_entry_tag timeout_event;
  opic_interrupt_source *interrupt_source;
} opic_timer;


/* the OPIC */

struct _hw_opic_device {

  /* vendor id */
  unsigned vendor_identification;

  /* interrupt destinations - processors */
  int nr_interrupt_destinations;
  opic_interrupt_destination *interrupt_destination;
  unsigned sizeof_interrupt_destination;

  /* bogus interrupts */
  int spurious_vector;

  /* interrupt sources - external interrupt source units + extra internal ones */
  int nr_interrupt_sources;
  opic_interrupt_source *interrupt_source;
  unsigned sizeof_interrupt_source;

  /* external interrupts */
  int nr_external_interrupts;
  opic_interrupt_source *external_interrupt_source;

  /* inter-processor-interrupts */
  int nr_interprocessor_interrupts;
  opic_interrupt_source *interprocessor_interrupt_source;

  /* timers */
  int nr_timer_interrupts;
  opic_timer *timer;
  unsigned sizeof_timer;
  opic_interrupt_source *timer_interrupt_source;
  unsigned timer_frequency;

  /* init register */
  unsigned32 init;

  /* address maps */
  opic_idu idu;
  int nr_isu_blocks;
  opic_isu_block *isu_block;
};


static void
hw_opic_init_data(device *me)
{
  hw_opic_device *opic = (hw_opic_device*)device_data(me);
  int isb;
  int idu_reg;
  int nr_isu_blocks;
  int i;

  /* determine the first valid reg property entry (there could be
     leading reg entries with invalid (zero) size fields) and the
     number of isu entries found in the reg property. */
  idu_reg = 0;
  nr_isu_blocks = 0;
  while (1) {
    reg_property_spec unit;
    int attach_space;
    unsigned_word attach_address;
    unsigned attach_size;
    if (!device_find_reg_array_property(me, "reg", idu_reg + nr_isu_blocks,
					&unit))
      break;
    if (nr_isu_blocks > 0
	|| (device_address_to_attach_address(device_parent(me), &unit.address,
					     &attach_space, &attach_address,
					     me)
	    && device_size_to_attach_size(device_parent(me), &unit.size,
					  &attach_size,
					  me))) {
      /* we count any thing once we've found one valid address/size pair */
      nr_isu_blocks += 1;
    }
    else {
      idu_reg += 1;
    }
  }

  /* determine the number and location of the multiple interrupt
     source units and the single interrupt delivery unit */
  if (opic->isu_block == NULL) {
    int reg_nr;
    opic->nr_isu_blocks = nr_isu_blocks;
    opic->isu_block = zalloc(sizeof(opic_isu_block) * opic->nr_isu_blocks);
    isb = 0;
    reg_nr = idu_reg;
    while (isb < opic->nr_isu_blocks) {
      reg_property_spec reg;
      if (!device_find_reg_array_property(me, "reg", reg_nr, &reg))
	device_error(me, "reg property missing entry number %d", reg_nr);
      opic->isu_block[isb].reg = reg_nr;
      if (!device_address_to_attach_address(device_parent(me), &reg.address,
					    &opic->isu_block[isb].space,
					    &opic->isu_block[isb].address,
					    me)
	  || !device_size_to_attach_size(device_parent(me), &reg.size,
					 &opic->isu_block[isb].size,
					 me)) {
	device_error(me, "reg property entry %d invalid", reg_nr);
      }
      if (!device_find_integer_array_property(me, "interrupt-ranges",
					      reg_nr * 2,
					      &opic->isu_block[isb].int_number)
	  || !device_find_integer_array_property(me, "interrupt-ranges",
						 reg_nr * 2 + 1,
						 &opic->isu_block[isb].range))
	device_error(me, "missing or invalid interrupt-ranges property entry %d", reg_nr);
      /* first reg entry specifies the address of both the IDU and the
         first set of ISU registers, adjust things accordingly */
      if (reg_nr == idu_reg) {
	opic->idu.reg = opic->isu_block[isb].reg;
	opic->idu.space = opic->isu_block[isb].space;
	opic->idu.address = opic->isu_block[isb].address;
	opic->idu.size = opic->isu_block[isb].size;
	opic->isu_block[isb].address += idu_isu_base;
	opic->isu_block[isb].size = opic->isu_block[isb].range * (16 + 16);
      }
      /* was this a valid reg entry? */
      if (opic->isu_block[isb].range == 0) {
	opic->nr_isu_blocks -= 1;
      }
      else {
	opic->nr_external_interrupts += opic->isu_block[isb].range;
	isb++;
      }
      reg_nr++;
    }
  }
  DTRACE(opic, ("interrupt source unit block - effective number of blocks %d\n",
		(int)opic->nr_isu_blocks));


  /* the number of other interrupts */
  opic->nr_interprocessor_interrupts = 4;
  opic->nr_timer_interrupts = 4;


  /* create space for the interrupt source registers */
  if (opic->interrupt_source != NULL) {
    memset(opic->interrupt_source, 0, opic->sizeof_interrupt_source);
  }
  else {
    opic->nr_interrupt_sources = (opic->nr_external_interrupts
				  + opic->nr_interprocessor_interrupts
				  + opic->nr_timer_interrupts);
    if (opic->nr_interrupt_sources > max_nr_interrupt_sources)
      device_error(me, "number of interrupt sources exceeded");
    opic->sizeof_interrupt_source = (sizeof(opic_interrupt_source)
				     * opic->nr_interrupt_sources);
    opic->interrupt_source = zalloc(opic->sizeof_interrupt_source);
    opic->external_interrupt_source = opic->interrupt_source;
    opic->interprocessor_interrupt_source = (opic->external_interrupt_source
					     + opic->nr_external_interrupts);
    opic->timer_interrupt_source = (opic->interprocessor_interrupt_source
				    + opic->nr_interprocessor_interrupts);
  }
  for (i = 0; i < opic->nr_interrupt_sources; i++) {
    opic_interrupt_source *source = &opic->interrupt_source[i];
    source->nr = i;
    source->is_masked = isu_mask_bit;
  }
  DTRACE(opic, ("interrupt sources - external %d, timer %d, ipi %d, total %d\n",
		opic->nr_external_interrupts,
		opic->nr_timer_interrupts,
		opic->nr_interprocessor_interrupts,
		opic->nr_interrupt_sources));


  /* timers or interprocessor interrupts */
  if (opic->timer != NULL)
    memset(opic->timer, 0, opic->sizeof_timer);
  else {
    opic->nr_timer_interrupts = 4;
    opic->sizeof_timer = sizeof(opic_timer) * opic->nr_timer_interrupts;
    opic->timer = zalloc(opic->sizeof_timer);
  }
  for (i = 0; i < opic->nr_timer_interrupts; i++) {
    opic_timer *timer = &opic->timer[i];
    timer->nr = i;
    timer->me = me;
    timer->opic = opic;
    timer->inhibited = 1;
    timer->interrupt_source = &opic->timer_interrupt_source[i];
  }
  if (device_find_property(me, "timer-frequency"))
    opic->timer_frequency = device_find_integer_property(me, "timer-frequency");
  else
    opic->timer_frequency = 1;


  /* create space for the interrupt destination registers */
  if (opic->interrupt_destination != NULL) {
    memset(opic->interrupt_destination, 0, opic->sizeof_interrupt_destination);
  }
  else {
    opic->nr_interrupt_destinations = tree_find_integer_property(me, "/openprom/options/smp");
    opic->sizeof_interrupt_destination = (sizeof(opic_interrupt_destination)
					  * opic->nr_interrupt_destinations);
    opic->interrupt_destination = zalloc(opic->sizeof_interrupt_destination);
    if (opic->nr_interrupt_destinations > max_nr_interrupt_destinations)
      device_error(me, "number of interrupt destinations exceeded");
  }
  for (i = 0; i < opic->nr_interrupt_destinations; i++) {
    opic_interrupt_destination *dest = &opic->interrupt_destination[i];
    dest->bit = (1 << i);
    dest->nr = i;
    dest->init_port = (device_interrupt_decode(me, "init0", output_port)
		       + i);
    dest->intr_port = (device_interrupt_decode(me, "intr0", output_port)
		       + i);
    dest->base_priority = max_nr_task_priorities - 1;
  }
  DTRACE(opic, ("interrupt destinations - total %d\n",
		(int)opic->nr_interrupt_destinations));
  

  /* verify and print out the ISU's */
  for (isb = 0; isb < opic->nr_isu_blocks; isb++) {
    unsigned correct_size;
    if ((opic->isu_block[isb].address % opic_alignment) != 0)
      device_error(me, "interrupt source unit %d address not aligned to %d byte boundary",
		   isb, opic_alignment);
    correct_size = opic->isu_block[isb].range * sizeof_isu_register_block;
    if (opic->isu_block[isb].size != correct_size)
      device_error(me, "interrupt source unit %d (reg %d) has an incorrect size, should be 0x%x",
		   isb, opic->isu_block[isb].reg, correct_size);
    DTRACE(opic, ("interrupt source unit block %ld - address %d:0x%lx, size 0x%lx, int-number %ld, range %ld\n",
		  (long)isb,
		  (int)opic->isu_block[isb].space,
		  (unsigned long)opic->isu_block[isb].address,
		  (unsigned long)opic->isu_block[isb].size,
		  (long)opic->isu_block[isb].int_number,
		  (long)opic->isu_block[isb].range));
  }


  /* verify and print out the IDU */
  {
    unsigned correct_size;
    unsigned alternate_size;
    if ((opic->idu.address % opic_alignment) != 0)
      device_error(me, "interrupt delivery unit not aligned to %d byte boundary",
		   opic_alignment);
    correct_size = (idu_per_processor_register_base
		    + (sizeof_idu_per_processor_register_block
		       * opic->nr_interrupt_destinations));
    alternate_size = (idu_per_processor_register_base
		      + (sizeof_idu_per_processor_register_block
			 * max_nr_interrupt_destinations));
    if (opic->idu.size != correct_size
	&& opic->idu.size != alternate_size)
      device_error(me, "interrupt delivery unit has incorrect size, should be 0x%x or 0x%x",
		   correct_size, alternate_size);
    DTRACE(opic, ("interrupt delivery unit - address %d:0x%lx, size 0x%lx\n",
		  (int)opic->idu.space,
		  (unsigned long)opic->idu.address,
		  (unsigned long)opic->idu.size));
  }

  /* initialize the init interrupts */
  opic->init = 0;


  /* vendor ident */
  if (device_find_property(me, "vendor-identification") != NULL)
    opic->vendor_identification = device_find_integer_property(me, "vendor-identification");
  else
    opic->vendor_identification = 0;

  /* misc registers */
  opic->spurious_vector = 0xff;

}


/* interrupt related actions */

static void
assert_interrupt(device *me,
		 hw_opic_device *opic,
		 opic_interrupt_destination *dest)
{
  ASSERT(dest >= opic->interrupt_destination);
  ASSERT(dest < opic->interrupt_destination + opic->nr_interrupt_destinations);
  DTRACE(opic, ("assert interrupt - intr port %d\n", dest->intr_port));
  device_interrupt_event(me, dest->intr_port, 1, NULL, 0);
}


static void
negate_interrupt(device *me,
		 hw_opic_device *opic,
		 opic_interrupt_destination *dest)
{
  ASSERT(dest >= opic->interrupt_destination);
  ASSERT(dest < opic->interrupt_destination + opic->nr_interrupt_destinations);
  DTRACE(opic, ("negate interrupt - intr port %d\n", dest->intr_port));
  device_interrupt_event(me, dest->intr_port, 0, NULL, 0);
}


static int
can_deliver(device *me,
	    opic_interrupt_source *source,
	    opic_interrupt_destination *dest)
{
  return (source != NULL && dest != NULL
	  && source->priority > dest->base_priority
	  && (dest->current_in_service == NULL
	      || source->priority > dest->current_in_service->priority));
}


static unsigned
deliver_pending(device *me,
		hw_opic_device *opic,
		opic_interrupt_destination *dest)
{
  ASSERT(can_deliver(me, dest->current_pending, dest));
  dest->current_in_service = dest->current_pending;
  dest->current_in_service->in_service |= dest->bit;
  if (!dest->current_pending->is_level_triggered) {
    if (dest->current_pending->is_multicast)
      dest->current_pending->pending &= ~dest->bit;
    else
      dest->current_pending->pending = 0;
  }
  dest->current_pending = NULL;
  negate_interrupt(me, opic, dest);
  return dest->current_in_service->vector;
}


typedef enum {
  pending_interrupt,
  in_service_interrupt,
} interrupt_class;

static opic_interrupt_source *
find_interrupt_for_dest(device *me,
			hw_opic_device *opic,
			opic_interrupt_destination *dest,
			interrupt_class class)
{
  int i;
  opic_interrupt_source *pending = NULL;
  for (i = 0; i < opic->nr_interrupt_sources; i++) {
    opic_interrupt_source *src = &opic->interrupt_source[i];
    /* is this a potential hit? */
    switch (class) {
    case in_service_interrupt:
      if ((src->in_service & dest->bit) == 0)
	continue;
      break;
    case pending_interrupt:
      if ((src->pending & dest->bit) == 0)
	continue;
      break;
    }
    /* see if it is the highest priority */
    if (pending == NULL)
      pending = src;
    else if (src->priority > pending->priority)
      pending = src;
  }
  return pending;
}


static opic_interrupt_destination *
find_lowest_dest(device *me, 
		 hw_opic_device *opic,
		 opic_interrupt_source *src)
{
  int i;
  opic_interrupt_destination *lowest = NULL;
  for (i = 0; i < opic->nr_interrupt_destinations; i++) {
    opic_interrupt_destination *dest = &opic->interrupt_destination[i];
    if (src->destination & dest->bit) {
      if (dest->base_priority < src->priority) {
	if (lowest == NULL)
	  lowest = dest;
	else if (lowest->base_priority > dest->base_priority)
	  lowest = dest;
	else if (lowest->current_in_service != NULL
		 && dest->current_in_service == NULL)
	  lowest = dest; /* not doing anything */
	else if (lowest->current_in_service != NULL
		 && dest->current_in_service != NULL
		 && (lowest->current_in_service->priority
		     > dest->current_in_service->priority))
	  lowest = dest; /* less urgent */
	/* FIXME - need to be more fair */	
      }
    }
  }
  return lowest;
}


static void
handle_interrupt(device *me,
		 hw_opic_device *opic,
		 opic_interrupt_source *src,
		 int asserted)
{
  if (src->is_masked) {
    DTRACE(opic, ("interrupt %d - ignore masked\n", src->nr));
  }
  else if (src->is_multicast) {
    /* always try to deliver multicast interrupts - just easier */
    int i;
    ASSERT(!src->is_level_triggered);
    ASSERT(src->is_positive_polarity);
    ASSERT(asserted);
    for (i = 0; i < opic->nr_interrupt_destinations; i++) {
      opic_interrupt_destination *dest = &opic->interrupt_destination[i];
      if (src->destination & dest->bit) {
	if (src->pending & dest->bit) {
	  DTRACE(opic, ("interrupt %d - multicast still pending to %d\n",
			src->nr, dest->nr));
	}
	else if (can_deliver(me, src, dest)) {
	  dest->current_pending = src;
	  src->pending |= dest->bit;
	  assert_interrupt(me, opic, dest);
	  DTRACE(opic, ("interrupt %d - multicast to %d\n",
			src->nr, dest->nr));
	}
	else {
	  src->pending |= dest->bit;
	  DTRACE(opic, ("interrupt %d - multicast pending to %d\n",
			src->nr, dest->nr));
	}
      }
    }
  }
  else if (src->is_level_triggered
	   && src->is_positive_polarity
	   && !asserted) {
    if (src->pending)
      DTRACE(opic, ("interrupt %d - ignore withdrawn (active high)\n",
		    src->nr));
    else
      DTRACE(opic, ("interrupt %d - ignore low level (active high)\n",
		    src->nr));
    ASSERT(!src->is_multicast);
    src->pending = 0;
  }
  else if (src->is_level_triggered
	   && !src->is_positive_polarity
	   && asserted) {
    if (src->pending)
      DTRACE(opic, ("interrupt %d - ignore withdrawn (active low)\n",
		    src->nr));
    else
      DTRACE(opic, ("interrupt %d - ignore high level (active low)\n",
		    src->nr));

    ASSERT(!src->is_multicast);
    src->pending = 0;
  }
  else if (!src->is_level_triggered
	   && src->is_positive_polarity
	   && !asserted) {
    DTRACE(opic, ("interrupt %d - ignore falling edge (positive edge trigered)\n",
		  src->nr));
  }
  else if (!src->is_level_triggered
	   && !src->is_positive_polarity
	   && asserted) {
    DTRACE(opic, ("interrupt %d - ignore rising edge (negative edge trigered)\n",
		  src->nr));
  }
  else if (src->in_service != 0) {
    /* leave the interrupt where it is */
    ASSERT(!src->is_multicast);
    ASSERT(src->pending == 0 || src->pending == src->in_service);
    src->pending = src->in_service;
    DTRACE(opic, ("interrupt %ld - ignore already in service to 0x%lx\n",
		  (long)src->nr, (long)src->in_service));
  }
  else if (src->pending != 0) {
    DTRACE(opic, ("interrupt %ld - ignore still pending to 0x%lx\n",
		  (long)src->nr, (long)src->pending));
  }
  else {
    /* delivery is needed */
    opic_interrupt_destination *dest = find_lowest_dest(me, opic, src);
    if (can_deliver(me, src, dest)) {
      dest->current_pending = src;
      src->pending = dest->bit;
      DTRACE(opic, ("interrupt %d - delivered to %d\n", src->nr, dest->nr));
      assert_interrupt(me, opic, dest);
    }
    else {
      src->pending = src->destination; /* any can take this */
      DTRACE(opic, ("interrupt %ld - pending to 0x%lx\n",
		    (long)src->nr, (long)src->pending));
    }
  }
}

static unsigned
do_interrupt_acknowledge_register_N_read(device *me,
					 hw_opic_device *opic,
					 int dest_nr)
{
  opic_interrupt_destination *dest = &opic->interrupt_destination[dest_nr];
  unsigned vector;

  ASSERT(dest_nr >= 0 && dest_nr < opic->nr_interrupt_destinations);
  ASSERT(dest_nr == dest->nr);

  /* try the current pending */
  if (can_deliver(me, dest->current_pending, dest)) {
    ASSERT(dest->current_pending->pending & dest->bit);
    vector = deliver_pending(me, opic, dest);
    DTRACE(opic, ("interrupt ack %d - entering %d (pending) - vector %d (%d), priority %d\n",
		  dest->nr,
		  dest->current_in_service->nr,
		  dest->current_in_service->vector, vector,
		  dest->current_in_service->priority));
  }
  else {
    /* try for something else */
    dest->current_pending = find_interrupt_for_dest(me, opic, dest, pending_interrupt);
    if (can_deliver(me, dest->current_pending, dest)) {
      vector = deliver_pending(me, opic, dest);    
      DTRACE(opic, ("interrupt ack %d - entering %d (not pending) - vector %d (%d), priority %d\n",
		    dest->nr,
		    dest->current_in_service->nr,
		    dest->current_in_service->vector, vector,
		    dest->current_in_service->priority));
    }
    else {
      dest->current_pending = NULL;
      vector = opic->spurious_vector;
      DTRACE(opic, ("interrupt ack %d - spurious interrupt %d\n",
		    dest->nr, vector));
    }
  }
  return vector;
}


static void
do_end_of_interrupt_register_N_write(device *me,
				     hw_opic_device *opic,
				     int dest_nr,
				     unsigned reg)
{
  opic_interrupt_destination *dest = &opic->interrupt_destination[dest_nr];

  ASSERT(dest_nr >= 0 && dest_nr < opic->nr_interrupt_destinations);
  ASSERT(dest_nr == dest->nr);

  /* check the value written is zero */
  if (reg != 0) {
    DTRACE(opic, ("eoi %d - ignoring nonzero value\n", dest->nr));
  }

  /* user doing wierd things? */
  if (dest->current_in_service == NULL) {
    DTRACE(opic, ("eoi %d - strange, no current interrupt\n", dest->nr));
    return;
  }

  /* an internal stuff up? */
  if (!(dest->current_in_service->in_service & dest->bit)) {
    device_error(me, "eoi %d - current interrupt not in service", dest->nr);
  }

  /* find what was probably the previous in service interrupt */
  dest->current_in_service->in_service &= ~dest->bit;
  DTRACE(opic, ("eoi %d - ending %d - priority %d, vector %d\n",
		dest->nr,
		dest->current_in_service->nr,
		dest->current_in_service->priority,
		dest->current_in_service->vector));
  dest->current_in_service = find_interrupt_for_dest(me, opic, dest, in_service_interrupt);
  if (dest->current_in_service != NULL)
    DTRACE(opic, ("eoi %d - resuming %d - priority %d, vector %d\n",
		  dest->nr,
		  dest->current_in_service->nr,
		  dest->current_in_service->priority,
		  dest->current_in_service->vector));
  else
    DTRACE(opic, ("eoi %d - resuming none\n", dest->nr));

  /* check to see if that shouldn't be interrupted */
  dest->current_pending = find_interrupt_for_dest(me, opic, dest, pending_interrupt);
  if (can_deliver(me, dest->current_pending, dest)) {
    ASSERT(dest->current_pending->pending & dest->bit);
    assert_interrupt(me, opic, dest);
  }
  else {
    dest->current_pending = NULL;
  }
}


static void
decode_opic_address(device *me,
		    hw_opic_device *opic,
		    int space,
		    unsigned_word address,
		    unsigned nr_bytes,
		    opic_register *type,
		    int *index)
{
  int isb = 0;

  /* is the size valid? */
  if (nr_bytes != 4) {
    *type = invalid_opic_register;
    *index = -1;
    return;
  }

  /* try for a per-processor register within the interrupt delivery
     unit */
  if (space == opic->idu.space
      && address >= (opic->idu.address + idu_per_processor_register_base)
      && address < (opic->idu.address + idu_per_processor_register_base
		    + (sizeof_idu_per_processor_register_block
		       * opic->nr_interrupt_destinations))) {
    unsigned_word block_offset = (address
				  - opic->idu.address
				  - idu_per_processor_register_base);
    unsigned_word offset = block_offset % sizeof_idu_per_processor_register_block;
    *index = block_offset / sizeof_idu_per_processor_register_block;
    switch (offset) {
    case 0x040:
      *type = ipi_N_dispatch_register;
      *index = 0;
      break;
    case 0x050:
      *type = ipi_N_dispatch_register;
      *index = 1;
      break;
    case 0x060:
      *type = ipi_N_dispatch_register;
      *index = 2;
      break;
    case 0x070:
      *type = ipi_N_dispatch_register;
      *index = 3;
      break;
    case 0x080:
      *type = current_task_priority_register_N;
      break;
    case 0x0a0:
      *type = interrupt_acknowledge_register_N;
      break;
    case 0x0b0:
      *type = end_of_interrupt_register_N;
      break;
    default:
      *type = invalid_opic_register;
      break;
    }
    DTRACE(opic, ("per-processor register %d:0x%lx - %s[%d]\n",
		  space, (unsigned long)address,
		  opic_register_name(*type),
		  *index));
    return;
  }

  /* try for an interrupt source unit */
  for (isb = 0; isb < opic->nr_isu_blocks; isb++) {
    if (opic->isu_block[isb].space == space
	&& address >= opic->isu_block[isb].address
	&& address < (opic->isu_block[isb].address + opic->isu_block[isb].size)) {
      unsigned_word block_offset = address - opic->isu_block[isb].address;
      unsigned_word offset = block_offset % sizeof_isu_register_block;
      *index = (opic->isu_block[isb].int_number
		+ (block_offset / sizeof_isu_register_block));
      switch (offset) {
      case 0x00:
	*type = interrupt_source_N_vector_priority_register;
	break;
      case 0x10:
	*type = interrupt_source_N_destination_register;
	break;
      default:
	*type = invalid_opic_register;
	break;
      }
      DTRACE(opic, ("isu register %d:0x%lx - %s[%d]\n",
		    space, (unsigned long)address,
		    opic_register_name(*type),
		    *index));
      return;
    }
  }

  /* try for a timer */
  if (space == opic->idu.space
      && address >= (opic->idu.address + idu_timer_base)
      && address < (opic->idu.address + idu_timer_base
		    + opic->nr_timer_interrupts * sizeof_timer_register_block)) {
    unsigned_word offset = address % sizeof_timer_register_block;
    *index = ((address - opic->idu.address - idu_timer_base)
	      / sizeof_timer_register_block);
    switch (offset) {
    case 0x00:
      *type = timer_N_current_count_register;
      break;
    case 0x10:
      *type = timer_N_base_count_register;
      break;
    case 0x20:
      *type = timer_N_vector_priority_register;
      break;
    case 0x30:
      *type = timer_N_destination_register;
      break;
    default:
      *type = invalid_opic_register;
      break;
    }
    DTRACE(opic, ("timer register %d:0x%lx - %s[%d]\n",
		  space, (unsigned long)address,
		  opic_register_name(*type),
		  *index));
    return;
  }

  /* finally some other misc global register */
  if (space == opic->idu.space
      && address >= opic->idu.address
      && address < opic->idu.address + opic->idu.size) {
    unsigned_word block_offset = address - opic->idu.address;
    switch (block_offset) {
    case 0x010f0:
      *type = timer_frequency_reporting_register;
      *index = -1;
      break;
    case 0x010e0:
      *type = spurious_vector_register;
      *index = -1;
      break;
    case 0x010d0:
    case 0x010c0:
    case 0x010b0:
    case 0x010a0:
      *type = ipi_N_vector_priority_register;
      *index = (block_offset - 0x010a0) / 16;
      break;
    case 0x01090:
      *type = processor_init_register;
      *index = -1;
      break;
    case 0x01080:
      *type = vendor_identification_register;
      *index = -1;
      break;
    case 0x01020:
      *type = global_configuration_register_N;
      *index = 0;
      break;
    case 0x01000:
      *type = feature_reporting_register_N;
      *index = 0;
      break;
    default:
      *type = invalid_opic_register;
      *index = -1;
      break;
    }
    DTRACE(opic, ("global register %d:0x%lx - %s[%d]\n",
		  space, (unsigned long)address,
		  opic_register_name(*type),
		  *index));
    return;
  }

  /* nothing matched */
  *type = invalid_opic_register;
  DTRACE(opic, ("invalid register %d:0x%lx\n",
		space, (unsigned long)address));
  return;
}


/* Processor init register:

   The bits in this register (one per processor) are directly wired to
   output "init" interrupt ports. */

static unsigned
do_processor_init_register_read(device *me,
				hw_opic_device *opic)
{
  unsigned reg = opic->init;
  DTRACE(opic, ("processor init register - read 0x%lx\n",
		(long)reg));
  return reg;
}

static void
do_processor_init_register_write(device *me,
				 hw_opic_device *opic,
				 unsigned reg)
{
  int i;
  for (i = 0; i < opic->nr_interrupt_destinations; i++) {
    opic_interrupt_destination *dest = &opic->interrupt_destination[i];
    if ((reg & dest->bit) != (opic->init & dest->bit)) {
      if (reg & dest->bit) {
	DTRACE(opic, ("processor init register - write 0x%lx - asserting init%d\n",
		      (long)reg, i));
	opic->init |= dest->bit;
	device_interrupt_event(me, dest->init_port, 1, NULL, 0);
      }
      else {
	DTRACE(opic, ("processor init register - write 0x%lx - negating init%d\n",
		      (long)reg, i));
	opic->init &= ~dest->bit;
	device_interrupt_event(me, dest->init_port, 0, NULL, 0);
      }
    }
  }
}



/* Interrupt Source Vector/Priority Register: */

static unsigned
read_vector_priority_register(device *me,
			      hw_opic_device *opic,
			      opic_interrupt_source *interrupt,
			      const char *reg_name,
			      int reg_index)
{
  unsigned reg;
  reg = 0;
  reg |= interrupt->is_masked;
  reg |= (interrupt->in_service || interrupt->pending
	  ? isu_active_bit : 0); /* active */
  reg |= interrupt->is_multicast;
  reg |= interrupt->is_positive_polarity;
  reg |= interrupt->is_level_triggered; /* sense? */
  reg |= interrupt->priority << isu_priority_shift;
  reg |= interrupt->vector;
  DTRACE(opic, ("%s %d vector/priority register - read 0x%lx\n",
		reg_name, reg_index, (unsigned long)reg));
  return reg;
}

static unsigned
do_interrupt_source_N_vector_priority_register_read(device *me,
						    hw_opic_device *opic,
						    int index)
{
  unsigned reg;
  ASSERT(index < opic->nr_external_interrupts);
  reg = read_vector_priority_register(me, opic,
				      &opic->interrupt_source[index],
				      "interrupt source", index);
  return reg;
}

static void
write_vector_priority_register(device *me,
			       hw_opic_device *opic,
			       opic_interrupt_source *interrupt,
			       unsigned reg,
			       const char *reg_name,
			       int reg_index)
{
  interrupt->is_masked = (reg & isu_mask_bit);
  interrupt->is_multicast = (reg & isu_multicast_bit);
  interrupt->is_positive_polarity = (reg & isu_positive_polarity_bit);
  interrupt->is_level_triggered = (reg & isu_level_triggered_bit);
  interrupt->priority = ((reg >> isu_priority_shift)
			 % max_nr_task_priorities);
  interrupt->vector = (reg & isu_vector_bits);
  DTRACE(opic, ("%s %d vector/priority register - write 0x%lx - %s%s%s-polarity, %s-triggered, priority %ld vector %ld\n",
		reg_name,
		reg_index,
		(unsigned long)reg,
		interrupt->is_masked ? "masked, " : "",
		interrupt->is_multicast ? "multicast, " : "",
		interrupt->is_positive_polarity ? "positive" : "negative",
		interrupt->is_level_triggered ? "level" : "edge",
		(long)interrupt->priority,
		(long)interrupt->vector));
}

static void
do_interrupt_source_N_vector_priority_register_write(device *me,
						     hw_opic_device *opic,
						     int index,
						     unsigned reg)
{
  ASSERT(index < opic->nr_external_interrupts);
  reg &= ~isu_multicast_bit; /* disable multicast */
  write_vector_priority_register(me, opic,
				 &opic->interrupt_source[index],
				 reg, "interrupt source", index);
}



/* Interrupt Source Destination Register: */

static unsigned
read_destination_register(device *me,
			  hw_opic_device *opic,
			  opic_interrupt_source *interrupt,
			  const char *reg_name,
			  int reg_index)
{
  unsigned long reg;
  reg = interrupt->destination;
  DTRACE(opic, ("%s %d destination register - read 0x%lx\n",
		reg_name, reg_index, reg));
  return reg;
}
			     
static unsigned
do_interrupt_source_N_destination_register_read(device *me,
						hw_opic_device *opic,
						int index)
{
  unsigned reg;
  ASSERT(index < opic->nr_external_interrupts);
  reg = read_destination_register(me, opic, &opic->external_interrupt_source[index],
				  "interrupt source", index);
  return reg;
}

static void
write_destination_register(device *me,
			   hw_opic_device *opic,
			   opic_interrupt_source *interrupt,
			   unsigned reg,
			   const char *reg_name,
			   int reg_index)
{
  reg &= (1 << opic->nr_interrupt_destinations) - 1; /* mask out invalid */
  DTRACE(opic, ("%s %d destination register - write 0x%x\n",
		reg_name, reg_index, reg));
  interrupt->destination = reg;
}

static void
do_interrupt_source_N_destination_register_write(device *me,
						 hw_opic_device *opic,
						 int index,
						 unsigned reg)
{
  ASSERT(index < opic->nr_external_interrupts);
  write_destination_register(me, opic, &opic->external_interrupt_source[index],
			     reg, "interrupt source", index);
}



/* Spurious vector register: */

static unsigned
do_spurious_vector_register_read(device *me,
				 hw_opic_device *opic)
{
  unsigned long reg = opic->spurious_vector;
  DTRACE(opic, ("spurious vector register - read 0x%lx\n", reg));
  return reg;
}

static void
do_spurious_vector_register_write(device *me,
				  hw_opic_device *opic,
				  unsigned reg)
{
  reg &= 0xff; /* mask off invalid */
  DTRACE(opic, ("spurious vector register - write 0x%x\n", reg));
  opic->spurious_vector = reg;
}



/* current task priority register: */

static unsigned
do_current_task_priority_register_N_read(device *me,
					 hw_opic_device *opic,
					 int index)
{
  opic_interrupt_destination *interrupt_destination = &opic->interrupt_destination[index];
  unsigned reg;
  ASSERT(index >= 0 && index < opic->nr_interrupt_destinations);
  reg = interrupt_destination->base_priority;
  DTRACE(opic, ("current task priority register %d - read 0x%x\n", index, reg));
  return reg;
}

static void
do_current_task_priority_register_N_write(device *me,
					  hw_opic_device *opic,
					  int index,
					  unsigned reg)
{
  opic_interrupt_destination *interrupt_destination = &opic->interrupt_destination[index];
  ASSERT(index >= 0 && index < opic->nr_interrupt_destinations);
  reg %= max_nr_task_priorities;
  DTRACE(opic, ("current task priority register %d - write 0x%x\n", index, reg));
  interrupt_destination->base_priority = reg;
}



/* Timer Frequency Reporting Register: */

static unsigned
do_timer_frequency_reporting_register_read(device *me,
					   hw_opic_device *opic)
{
  unsigned reg;
  reg = opic->timer_frequency;
  DTRACE(opic, ("timer frequency reporting register - read 0x%x\n", reg));
  return reg;
}

static void
do_timer_frequency_reporting_register_write(device *me,
					    hw_opic_device *opic,
					    unsigned reg)
{
  DTRACE(opic, ("timer frequency reporting register - write 0x%x\n", reg));
  opic->timer_frequency = reg;
}


/* timer registers: */

static unsigned
do_timer_N_current_count_register_read(device *me,
				       hw_opic_device *opic,
				       int index)
{
  opic_timer *timer = &opic->timer[index];
  unsigned reg;
  ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  if (timer->inhibited)
    reg = timer->count; /* stalled value */
  else
    reg = timer->count - device_event_queue_time(me); /* time remaining */
  DTRACE(opic, ("timer %d current count register - read 0x%x\n", index, reg));
  return reg;
}


static unsigned
do_timer_N_base_count_register_read(device *me,
				    hw_opic_device *opic,
				    int index)
{
  opic_timer *timer = &opic->timer[index];
  unsigned reg;
  ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  reg = timer->base_count;
  DTRACE(opic, ("timer %d base count register - read 0x%x\n", index, reg));
  return reg;
}


static void
timer_event(void *data)
{
  opic_timer *timer = data;
  device *me = timer->me;
  if (timer->inhibited)
    device_error(timer->me, "internal-error - timer event occured when timer %d inhibited",
		 timer->nr);
  handle_interrupt(timer->me, timer->opic, timer->interrupt_source, 1);
  timer->timeout_event = device_event_queue_schedule(me, timer->base_count,
						     timer_event, timer);
  DTRACE(opic, ("timer %d - interrupt at %ld, next at %d\n",
		timer->nr, (long)device_event_queue_time(me), timer->base_count));
}


static void
do_timer_N_base_count_register_write(device *me,
				     hw_opic_device *opic,
				     int index,
				     unsigned reg)
{
  opic_timer *timer = &opic->timer[index];
  int inhibit;
  ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  inhibit = reg & 0x80000000;
  if (timer->inhibited && !inhibit) {
    timer->inhibited = 0;
    if (timer->timeout_event != NULL)
      device_event_queue_deschedule(me, timer->timeout_event);
    timer->count = device_event_queue_time(me) + reg;
    timer->base_count = reg;
    timer->timeout_event = device_event_queue_schedule(me, timer->base_count,
						       timer_event, (void*)timer);
    DTRACE(opic, ("timer %d base count register - write 0x%x - timer started\n",
		  index, reg));
  }
  else if (!timer->inhibited && inhibit) {
    if (timer->timeout_event != NULL)
      device_event_queue_deschedule(me, timer->timeout_event);
    timer->count = timer->count - device_event_queue_time(me);
    timer->inhibited = 1;
    timer->base_count = reg;
    DTRACE(opic, ("timer %d base count register - write 0x%x - timer stopped\n",
		  index, reg));
  }
  else {
    ASSERT((timer->inhibited && inhibit) || (!timer->inhibited && !inhibit));
    DTRACE(opic, ("timer %d base count register - write 0x%x\n", index, reg));
    timer->base_count = reg;
  }
}


static unsigned
do_timer_N_vector_priority_register_read(device *me,
					 hw_opic_device *opic,
					 int index)
{
  unsigned reg;
  ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  reg = read_vector_priority_register(me, opic,
				      &opic->timer_interrupt_source[index],
				      "timer", index);
  return reg;
}

static void
do_timer_N_vector_priority_register_write(device *me,
					  hw_opic_device *opic,
					  int index,
					  unsigned reg)
{
  ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  reg &= ~isu_level_triggered_bit; /* force edge trigger */
  reg |= isu_positive_polarity_bit; /* force rising (positive) edge */
  reg |= isu_multicast_bit; /* force multicast */
  write_vector_priority_register(me, opic,
				 &opic->timer_interrupt_source[index],
				 reg, "timer", index);
}


static unsigned
do_timer_N_destination_register_read(device *me,
				     hw_opic_device *opic,
				     int index)
{
  unsigned reg;
  ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  reg = read_destination_register(me, opic, &opic->timer_interrupt_source[index],
				  "timer", index);
  return reg;
}

static void
do_timer_N_destination_register_write(device *me,
				      hw_opic_device *opic,
				      int index,
				      unsigned reg)
{
  ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  write_destination_register(me, opic, &opic->timer_interrupt_source[index],
			     reg, "timer", index);
}


/* IPI registers */

static unsigned
do_ipi_N_vector_priority_register_read(device *me,
				       hw_opic_device *opic,
				       int index)
{
  unsigned reg;
  ASSERT(index >= 0 && index < opic->nr_interprocessor_interrupts);
  reg = read_vector_priority_register(me, opic,
				      &opic->interprocessor_interrupt_source[index],
				      "ipi", index);
  return reg;
}

static void
do_ipi_N_vector_priority_register_write(device *me,
					hw_opic_device *opic,
					int index,
					unsigned reg)
{
  ASSERT(index >= 0 && index < opic->nr_interprocessor_interrupts);
  reg &= ~isu_level_triggered_bit; /* force edge trigger */
  reg |= isu_positive_polarity_bit; /* force rising (positive) edge */
  reg |= isu_multicast_bit; /* force a multicast source */
  write_vector_priority_register(me, opic,
				 &opic->interprocessor_interrupt_source[index],
				 reg, "ipi", index);
}

static void
do_ipi_N_dispatch_register_write(device *me,
				 hw_opic_device *opic,
				 int index,
				 unsigned reg)
{
  opic_interrupt_source *source = &opic->interprocessor_interrupt_source[index];
  ASSERT(index >= 0 && index < opic->nr_interprocessor_interrupts);
  DTRACE(opic, ("ipi %d interrupt dispatch register - write 0x%x\n", index, reg));
  source->destination = reg;
  handle_interrupt(me, opic, source, 1);
}


/* vendor and other global registers */

static unsigned
do_vendor_identification_register_read(device *me,
				       hw_opic_device *opic)
{
  unsigned reg;
  reg = opic->vendor_identification;
  DTRACE(opic, ("vendor identification register - read 0x%x\n", reg));
  return reg;
}

static unsigned
do_feature_reporting_register_N_read(device *me,
				     hw_opic_device *opic,
				     int index)
{
  unsigned reg = 0;
  ASSERT(index == 0);
  switch (index) {
  case 0:
    reg |= (opic->nr_external_interrupts << 16);
    reg |= (opic->nr_interrupt_destinations << 8);
    reg |= (2/*version 1.2*/);
    break;
  }
  DTRACE(opic, ("feature reporting register %d - read 0x%x\n", index, reg));
  return reg;
}

static unsigned
do_global_configuration_register_N_read(device *me,
					hw_opic_device *opic,
					int index)
{
  unsigned reg = 0;
  ASSERT(index == 0);
  switch (index) {
  case 0:
    reg |= gcr0_8259_bit; /* hardwire 8259 disabled */
    break;
  }
  DTRACE(opic, ("global configuration register %d - read 0x%x\n", index, reg));
  return reg;
}

static void
do_global_configuration_register_N_write(device *me,
					 hw_opic_device *opic,
					 int index,
					 unsigned reg)
{
  ASSERT(index == 0);
  if (reg & gcr0_reset_bit) {
    DTRACE(opic, ("global configuration register %d - write 0x%x - reseting opic\n", index, reg));
    hw_opic_init_data(me);
  }
  if (!(reg & gcr0_8259_bit)) {
    DTRACE(opic, ("global configuration register %d - write 0x%x - ignoring 8259 enable\n", index, reg));
  }
}



/* register read-write */

static unsigned
hw_opic_io_read_buffer(device *me,
		       void *dest,
		       int space,
		       unsigned_word addr,
		       unsigned nr_bytes,
		       cpu *processor,
		       unsigned_word cia)
{
  hw_opic_device *opic = (hw_opic_device*)device_data(me);
  opic_register type;
  int index;
  decode_opic_address(me, opic, space, addr, nr_bytes, &type, &index);
  if (type == invalid_opic_register) {
    device_error(me, "invalid opic read access to %d:0x%lx (%d bytes)",
		 space, (unsigned long)addr, nr_bytes);
  }
  else {
    unsigned reg;
    switch (type) {
    case processor_init_register:
      reg = do_processor_init_register_read(me, opic);
      break;
    case interrupt_source_N_vector_priority_register:
      reg = do_interrupt_source_N_vector_priority_register_read(me, opic, index);
      break;
    case interrupt_source_N_destination_register:
      reg = do_interrupt_source_N_destination_register_read(me, opic, index);
      break;
    case interrupt_acknowledge_register_N:
      reg = do_interrupt_acknowledge_register_N_read(me, opic, index);
      break;
    case spurious_vector_register:
      reg = do_spurious_vector_register_read(me, opic);
      break;
    case current_task_priority_register_N:
      reg = do_current_task_priority_register_N_read(me, opic, index);
      break;
    case timer_frequency_reporting_register:
      reg = do_timer_frequency_reporting_register_read(me, opic);
      break;
    case timer_N_current_count_register:
      reg = do_timer_N_current_count_register_read(me, opic, index);
      break;
    case timer_N_base_count_register:
      reg = do_timer_N_base_count_register_read(me, opic, index);
      break;
    case timer_N_vector_priority_register:
      reg = do_timer_N_vector_priority_register_read(me, opic, index);
      break;
    case timer_N_destination_register:
      reg = do_timer_N_destination_register_read(me, opic, index);
      break;
    case ipi_N_vector_priority_register:
      reg = do_ipi_N_vector_priority_register_read(me, opic, index);
      break;
    case feature_reporting_register_N:
      reg = do_feature_reporting_register_N_read(me, opic, index);
      break;
    case global_configuration_register_N:
      reg = do_global_configuration_register_N_read(me, opic, index);
      break;
    case vendor_identification_register:
      reg = do_vendor_identification_register_read(me, opic);
      break;
    default:
      reg = 0;
      device_error(me, "unimplemented read of register %s[%d]",
		   opic_register_name(type), index);
    }
    *(unsigned_4*)dest = H2LE_4(reg);
  }
  return nr_bytes;
}


static unsigned
hw_opic_io_write_buffer(device *me,
			const void *source,
			int space,
			unsigned_word addr,
			unsigned nr_bytes,
			cpu *processor,
			unsigned_word cia)
{
  hw_opic_device *opic = (hw_opic_device*)device_data(me);
  opic_register type;
  int index;
  decode_opic_address(me, opic, space, addr, nr_bytes, &type, &index);
  if (type == invalid_opic_register) {
    device_error(me, "invalid opic write access to %d:0x%lx (%d bytes)",
		 space, (unsigned long)addr, nr_bytes);
  }
  else {
    unsigned reg = LE2H_4(*(unsigned_4*)source);
    switch (type) {
    case processor_init_register:
      do_processor_init_register_write(me, opic, reg);
      break;
    case interrupt_source_N_vector_priority_register:
      do_interrupt_source_N_vector_priority_register_write(me, opic, index, reg);
      break;
    case interrupt_source_N_destination_register:
      do_interrupt_source_N_destination_register_write(me, opic, index, reg);
      break;
    case end_of_interrupt_register_N:
      do_end_of_interrupt_register_N_write(me, opic, index, reg);
      break;
    case spurious_vector_register:
      do_spurious_vector_register_write(me, opic, reg);
      break;
    case current_task_priority_register_N:
      do_current_task_priority_register_N_write(me, opic, index, reg);
      break;
    case timer_frequency_reporting_register:
      do_timer_frequency_reporting_register_write(me, opic, reg);
      break;
    case timer_N_base_count_register:
      do_timer_N_base_count_register_write(me, opic, index, reg);
      break;
    case timer_N_vector_priority_register:
      do_timer_N_vector_priority_register_write(me, opic, index, reg);
      break;
    case timer_N_destination_register:
      do_timer_N_destination_register_write(me, opic, index, reg);
      break;
    case ipi_N_dispatch_register:
      do_ipi_N_dispatch_register_write(me, opic, index, reg);
      break;
    case ipi_N_vector_priority_register:
      do_ipi_N_vector_priority_register_write(me, opic, index, reg);
      break;
    case global_configuration_register_N:
      do_global_configuration_register_N_write(me, opic, index, reg);
      break;
    default:
      device_error(me, "unimplemented write to register %s[%d]",
		   opic_register_name(type), index);
    }
  }
  return nr_bytes;
}
  
  
static void
hw_opic_interrupt_event(device *me,
			int my_port,
			device *source,
			int source_port,
			int level,
			cpu *processor,
			unsigned_word cia)
{
  hw_opic_device *opic = (hw_opic_device*)device_data(me);

  int isb;
  int src_nr = 0;

  /* find the corresponding internal input port */
  for (isb = 0; isb < opic->nr_isu_blocks; isb++) {
    if (my_port >= opic->isu_block[isb].int_number
	&& my_port < opic->isu_block[isb].int_number + opic->isu_block[isb].range) {
      src_nr += my_port - opic->isu_block[isb].int_number;
      break;
    }
    else
      src_nr += opic->isu_block[isb].range;
  }
  if (isb == opic->nr_isu_blocks)
    device_error(me, "interrupt %d out of range", my_port);
  DTRACE(opic, ("external-interrupt %d, internal %d, level %d\n",
		my_port, src_nr, level));

  /* pass it on */
  ASSERT(src_nr >= 0 && src_nr < opic->nr_external_interrupts);
  handle_interrupt(me, opic, &opic->external_interrupt_source[src_nr], level);
}


static const device_interrupt_port_descriptor hw_opic_interrupt_ports[] = {
  { "irq", 0, max_nr_interrupt_sources, input_port, },
  { "intr", 0, max_nr_interrupt_destinations, output_port, },
  { "init", max_nr_interrupt_destinations, max_nr_interrupt_destinations, output_port, },
  { NULL }
};


static device_callbacks const hw_opic_callbacks = {
  { generic_device_init_address,
    hw_opic_init_data },
  { NULL, }, /* address */
  { hw_opic_io_read_buffer,
    hw_opic_io_write_buffer }, /* IO */
  { NULL, }, /* DMA */
  { hw_opic_interrupt_event, NULL, hw_opic_interrupt_ports }, /* interrupt */
  { NULL, }, /* unit */
  NULL, /* instance */
};

static void *
hw_opic_create(const char *name,
	       const device_unit *unit_address,
	       const char *args)
{
  hw_opic_device *opic = ZALLOC(hw_opic_device);
  return opic;
}



const device_descriptor hw_opic_device_descriptor[] = {
  { "opic", hw_opic_create, &hw_opic_callbacks },
  { NULL },
};

#endif /* _HW_OPIC_C_ */