Subversion Repositories Kolibri OS

Rev

Rev 3768 | Rev 3774 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
2455 mario79 3
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
2288 clevermous 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
8
IRQ_RESERVED   equ 24
9
 
10
IRQ_POOL_SIZE  equ 48
11
 
12
uglobal
13
 
14
align 16
2384 hidnplayr 15
irqh_tab            rd sizeof.LHEAD * IRQ_RESERVED / 4
2288 clevermous 16
 
2384 hidnplayr 17
irqh_pool           rd sizeof.IRQH * IRQ_POOL_SIZE /4
2288 clevermous 18
next_irqh           rd 1
19
 
20
irq_active_set      rd 1
21
irq_failed          rd IRQ_RESERVED
22
 
23
endg
24
 
25
align 4
26
init_irqs:
27
 
28
        mov     ecx, IRQ_RESERVED
29
        mov     edi, irqh_tab
30
@@:
31
        mov     eax, edi
32
        stosd
33
        stosd
34
        loop    @B
35
 
36
        mov     ecx, IRQ_POOL_SIZE-1
2384 hidnplayr 37
        mov     eax, irqh_pool+sizeof.IRQH
2288 clevermous 38
        mov     [next_irqh], irqh_pool
39
@@:
2384 hidnplayr 40
        mov     [eax-sizeof.IRQH], eax
41
        add     eax, sizeof.IRQH
2288 clevermous 42
        loop    @B
43
 
2384 hidnplayr 44
        mov     [eax-sizeof.IRQH], dword 0
2288 clevermous 45
        ret
46
 
47
 
48
align 4
49
proc attach_int_handler stdcall, irq:dword, handler:dword, user_data:dword
50
         locals
51
           .irqh dd ?
52
         endl
53
 
54
        and     [.irqh], 0
55
 
56
        push    ebx
57
 
58
        mov     ebx, [irq]          ;irq num
59
        test    ebx, ebx
60
        jz      .err
61
 
62
        cmp     ebx, IRQ_RESERVED
63
        jae     .err
64
 
65
        mov     edx, [handler]
66
        test    edx, edx
67
        jz      .err
68
 
3505 clevermous 69
        spin_lock_irqsave IrqsList
2288 clevermous 70
 
71
;allocate handler
72
 
73
        mov     ecx, [next_irqh]
74
        test    ecx, ecx
75
        jz      .fail
76
 
77
        mov     eax, [ecx]
78
        mov     [next_irqh], eax
79
        mov     [.irqh], ecx
80
 
81
        mov     [irq_failed+ebx*4], 0;clear counter
82
 
83
        mov     eax, [user_data]
84
        mov     [ecx+IRQH.handler], edx
85
        mov     [ecx+IRQH.data], eax
3505 clevermous 86
        and     [ecx+IRQH.num_ints], 0
2288 clevermous 87
 
88
        lea     edx, [irqh_tab+ebx*8]
3505 clevermous 89
        list_add_tail ecx, edx     ;clobber eax
2288 clevermous 90
        stdcall enable_irq, ebx
91
 
92
.fail:
3505 clevermous 93
        spin_unlock_irqrestore IrqsList
2288 clevermous 94
.err:
95
        pop     ebx
96
        mov     eax, [.irqh]
97
        ret
98
 
99
endp
100
 
101
if 0
102
align 4
103
proc get_int_handler stdcall, irq:dword
104
 
105
        mov     eax, [irq]
106
        cmp     eax, 15
107
        ja      .fail
108
        mov     eax, [irq_tab + 4 * eax]
109
        ret
110
.fail:
111
        xor     eax, eax
112
        ret
113
endp
114
end if
115
 
116
 
117
align 4
118
proc  detach_int_handler
119
 
120
        ret
121
endp
122
 
123
 
124
macro irq_serv_h [num] {
125
    forward
126
align 4
127
  .irq_#num :
128
        push    num
129
        jmp     .main
130
}
131
 
132
align 16
133
irq_serv:
134
 
135
; .irq_1:
136
;      push 1
137
;      jmp .main
138
; etc...
139
 
140
irq_serv_h 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15
141
irq_serv_h 16, 17, 18, 19, 20, 21, 22, 23
142
 
143
purge irq_serv_h
144
 
145
align 16
146
.main:
3505 clevermous 147
        save_ring3_context
2288 clevermous 148
 
149
        mov     ebp, [esp + 32]
150
        mov     bx, app_data;os_data
151
        mov     ds, bx
152
        mov     es, bx
153
 
154
        cmp     [v86_irqhooks+ebp*8], 0
155
        jnz     v86_irq
156
 
157
        bts     [irq_active_set], ebp
158
 
159
        lea     esi, [irqh_tab+ebp*8]   ; esi= list head
160
        mov     ebx, esi
161
.next:
162
        mov     ebx, [ebx+IRQH.list.next]; ebx= irqh pointer
163
        cmp     ebx, esi
164
        je      .done
165
 
166
        push    ebx                     ; FIX THIS
167
        push    edi
168
        push    esi
169
 
170
        push    [ebx+IRQH.data]
171
        call    [ebx+IRQH.handler]
3505 clevermous 172
        pop     ecx
2288 clevermous 173
 
174
        pop     esi
175
        pop     edi
176
        pop     ebx
177
 
178
        test    eax, eax
179
        jz      .next
180
 
3505 clevermous 181
        inc     [ebx+IRQH.num_ints]
2288 clevermous 182
        btr     [irq_active_set], ebp
183
        jmp     .next
184
 
185
.done:
186
        btr     [irq_active_set], ebp
187
        jnc     .exit
188
 
3505 clevermous 189
; There is at least one configuration with one device which generates IRQ
190
; that is not the same as it should be according to PCI config space.
191
; For that device, the handler is registered at wrong IRQ.
192
; As a workaround, when nobody acknowledges the generated IRQ,
193
; try to ask all other registered handlers; if some handler acknowledges
194
; the IRQ this time, relink it to the current IRQ list.
195
; To make this more reliable, for every handler keep number of times
196
; that it has acknowledged an IRQ, and assume that handlers with at least one
197
; acknowledged IRQ are registered properly.
198
; Note: this still isn't 100% correct, because two IRQs can fire simultaneously,
199
; the better way would be to find the correct IRQ, but I don't know how to do
200
; this in that case.
201
        push    ebp
202
        xor     ebp, ebp
203
.try_other_irqs:
204
        cmp     ebp, [esp]
205
        jz      .try_next_irq
3543 clevermous 206
        cmp     ebp, 1
207
        jz      .try_next_irq
3771 mario79 208
        cmp     ebp, 6
209
        jz      .try_next_irq
3543 clevermous 210
        cmp     ebp, 12
211
        jz      .try_next_irq
3762 mario79 212
        cmp     ebp, 14
213
        jz      .try_next_irq
214
        cmp     ebp, 15
215
        jz      .try_next_irq
3505 clevermous 216
        lea     esi, [irqh_tab+ebp*8]
217
        mov     ebx, esi
218
.try_next_handler:
219
        mov     ebx, [ebx+IRQH.list.next]
220
        cmp     ebx, esi
221
        je      .try_next_irq
222
        cmp     [ebx+IRQH.num_ints], 0
3512 clevermous 223
        jne     .try_next_handler
3509 clevermous 224
; keyboard handler acknowledges everything
3505 clevermous 225
        push    [ebx+IRQH.data]
226
        call    [ebx+IRQH.handler]
227
        pop     ecx
228
        test    eax, eax
3511 clevermous 229
        jz      .try_next_handler
230
 
231
.found_in_wrong_list:
232
        DEBUGF 1,'K : warning: relinking handler from IRQ%d to IRQ%d\n',\
233
                ebp, [esp]
3513 clevermous 234
        pop     ebp
3511 clevermous 235
        spin_lock_irqsave IrqsList
236
        list_del ebx
237
        lea     edx, [irqh_tab+ebp*8]
238
        list_add_tail ebx, edx
239
        spin_unlock_irqrestore IrqsList
240
        jmp     .exit
241
 
3505 clevermous 242
.try_next_irq:
243
        inc     ebp
244
        cmp     ebp, 16
245
        jb      .try_other_irqs
246
        pop     ebp
247
 
3509 clevermous 248
.fail:
2288 clevermous 249
        inc     [irq_failed+ebp*4]
250
.exit:
251
 
252
        mov     ecx, ebp
253
        call    irq_eoi
254
 
3615 clevermous 255
; IRQ handler could make some kernel thread ready; reschedule
256
        mov     bl, SCHEDULE_HIGHER_PRIORITY
257
        call    find_next_task
258
        jz      .return  ; if there is only one running process
259
        call    do_change_task
260
  .return:
3505 clevermous 261
        restore_ring3_context
2288 clevermous 262
        add     esp, 4
263
        iret
264
 
265
align 4
266
irqD:
267
        push    eax
268
        push    ecx
269
        xor     eax, eax
270
        out     0xf0, al
271
        mov     cl, 13
272
        call    irq_eoi
273
        pop     ecx
274
        pop     eax
275
        iret
276