-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhw_opic.c
1822 lines (1580 loc) · 53 KB
/
hw_opic.c
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef _HW_OPIC_C_
#define _HW_OPIC_C_
#include "device_table.h"
#include <string.h>
/* 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;
int64_t 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 */
uint32_t 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, ®))
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), ®.address,
&opic->isu_block[isb].space,
&opic->isu_block[isb].address,
me)
|| !device_size_to_attach_size(device_parent(me), ®.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,
(signed_cell *)
&opic->isu_block[isb].int_number)
|| !device_find_integer_array_property(me, "interrupt-ranges",
reg_nr * 2 + 1,
(signed_cell *)
&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 triggered)\n",
src->nr));
}
else if (!src->is_level_triggered
&& !src->is_positive_polarity
&& asserted) {
DTRACE(opic, ("interrupt %d - ignore rising edge (negative edge triggered)\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 weird 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;