2011-10-14 23:38:50 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2024-05-22 17:15:14 +02:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2024. All rights reserved. ;;
|
2011-10-14 23:38:50 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-04-17 23:19:45 +02:00
|
|
|
|
2020-03-02 05:25:30 +01:00
|
|
|
IRQ_RESERVED = 56
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2017-12-20 02:07:10 +01:00
|
|
|
IRQ_POOL_SIZE = 48
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
uglobal
|
|
|
|
|
|
|
|
align 16
|
2012-02-23 19:52:13 +01:00
|
|
|
irqh_tab rd sizeof.LHEAD * IRQ_RESERVED / 4
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2012-02-23 19:52:13 +01:00
|
|
|
irqh_pool rd sizeof.IRQH * IRQ_POOL_SIZE /4
|
2011-10-14 23:38:50 +02:00
|
|
|
next_irqh rd 1
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
irq_active_set rd (IRQ_RESERVED + 31)/32
|
2011-10-14 23:38:50 +02:00
|
|
|
irq_failed rd IRQ_RESERVED
|
|
|
|
|
|
|
|
endg
|
|
|
|
|
2020-03-02 05:25:30 +01:00
|
|
|
set_irq_active:
|
|
|
|
mov eax, ebp
|
|
|
|
mov ecx, ebp
|
|
|
|
shr ecx, 5
|
|
|
|
and eax, 31
|
2022-02-12 18:27:41 +01:00
|
|
|
bts [irq_active_set + ecx*4], eax
|
2020-03-02 05:25:30 +01:00
|
|
|
ret
|
|
|
|
|
|
|
|
reset_irq_active:
|
|
|
|
mov eax, ebp
|
|
|
|
mov ecx, ebp
|
|
|
|
shr ecx, 5
|
|
|
|
and eax, 31
|
2022-02-12 18:27:41 +01:00
|
|
|
btr [irq_active_set + ecx*4], eax
|
2020-03-02 05:25:30 +01:00
|
|
|
ret
|
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
align 4
|
|
|
|
init_irqs:
|
|
|
|
|
|
|
|
mov ecx, IRQ_RESERVED
|
|
|
|
mov edi, irqh_tab
|
|
|
|
@@:
|
|
|
|
mov eax, edi
|
|
|
|
stosd
|
|
|
|
stosd
|
|
|
|
loop @B
|
|
|
|
|
|
|
|
mov ecx, IRQ_POOL_SIZE-1
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, irqh_pool + sizeof.IRQH
|
2011-10-14 23:38:50 +02:00
|
|
|
mov [next_irqh], irqh_pool
|
|
|
|
@@:
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [eax - sizeof.IRQH], eax
|
2012-02-23 19:52:13 +01:00
|
|
|
add eax, sizeof.IRQH
|
2011-10-14 23:38:50 +02:00
|
|
|
loop @B
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [eax - sizeof.IRQH], dword 0
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc attach_int_handler stdcall, irq:dword, handler:dword, user_data:dword
|
|
|
|
locals
|
|
|
|
.irqh dd ?
|
|
|
|
endl
|
2013-07-09 14:38:05 +02:00
|
|
|
|
2013-07-07 04:10:50 +02:00
|
|
|
DEBUGF 1, "K : Attach Interrupt %d Handler %x\n", [irq], [handler]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
and [.irqh], 0
|
|
|
|
|
|
|
|
push ebx
|
|
|
|
|
|
|
|
mov ebx, [irq] ;irq num
|
|
|
|
test ebx, ebx
|
|
|
|
jz .err
|
|
|
|
|
|
|
|
cmp ebx, IRQ_RESERVED
|
|
|
|
jae .err
|
|
|
|
|
|
|
|
mov edx, [handler]
|
|
|
|
test edx, edx
|
|
|
|
jz .err
|
|
|
|
|
2013-05-15 17:10:55 +02:00
|
|
|
spin_lock_irqsave IrqsList
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
;allocate handler
|
|
|
|
|
|
|
|
mov ecx, [next_irqh]
|
|
|
|
test ecx, ecx
|
|
|
|
jz .fail
|
|
|
|
|
|
|
|
mov eax, [ecx]
|
|
|
|
mov [next_irqh], eax
|
|
|
|
mov [.irqh], ecx
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [irq_failed + ebx*4], 0;clear counter
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov eax, [user_data]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [ecx + IRQH.handler], edx
|
|
|
|
mov [ecx + IRQH.data], eax
|
|
|
|
and [ecx + IRQH.num_ints], 0
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea edx, [irqh_tab + ebx*8]
|
2013-05-15 17:10:55 +02:00
|
|
|
list_add_tail ecx, edx ;clobber eax
|
2011-10-14 23:38:50 +02:00
|
|
|
stdcall enable_irq, ebx
|
|
|
|
|
|
|
|
.fail:
|
2013-05-15 17:10:55 +02:00
|
|
|
spin_unlock_irqrestore IrqsList
|
2011-10-14 23:38:50 +02:00
|
|
|
.err:
|
|
|
|
pop ebx
|
|
|
|
mov eax, [.irqh]
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
if 0
|
|
|
|
align 4
|
|
|
|
proc get_int_handler stdcall, irq:dword
|
|
|
|
|
|
|
|
mov eax, [irq]
|
|
|
|
cmp eax, 15
|
|
|
|
ja .fail
|
|
|
|
mov eax, [irq_tab + 4 * eax]
|
|
|
|
ret
|
|
|
|
.fail:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
end if
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc detach_int_handler
|
|
|
|
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
macro irq_serv_h [num] {
|
|
|
|
forward
|
|
|
|
align 4
|
|
|
|
.irq_#num :
|
|
|
|
push num
|
|
|
|
jmp .main
|
|
|
|
}
|
|
|
|
|
|
|
|
align 16
|
|
|
|
irq_serv:
|
|
|
|
|
2020-03-02 05:25:30 +01:00
|
|
|
rept 12 irqn:1 {irq_serv_h irqn} ; 1--12
|
|
|
|
rept 18 irqn:14 {irq_serv_h irqn} ; 14--31 (irq32 is vector 0x40)
|
|
|
|
rept 23 irqn:33 {irq_serv_h irqn} ; 33--55
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
purge irq_serv_h
|
|
|
|
|
|
|
|
align 16
|
|
|
|
.main:
|
2013-05-15 17:10:55 +02:00
|
|
|
save_ring3_context
|
2011-10-14 23:38:50 +02:00
|
|
|
mov ebp, [esp + 32]
|
|
|
|
mov bx, app_data;os_data
|
|
|
|
mov ds, bx
|
|
|
|
mov es, bx
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
cmp [v86_irqhooks + ebp*8], 0
|
2011-10-14 23:38:50 +02:00
|
|
|
jnz v86_irq
|
|
|
|
|
2020-03-02 05:25:30 +01:00
|
|
|
call set_irq_active
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea esi, [irqh_tab + ebp*8] ; esi= list head
|
2011-10-14 23:38:50 +02:00
|
|
|
mov ebx, esi
|
|
|
|
.next:
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ebx, [ebx + IRQH.list.next]; ebx= irqh pointer
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp ebx, esi
|
|
|
|
je .done
|
|
|
|
|
|
|
|
push ebx ; FIX THIS
|
|
|
|
push edi
|
|
|
|
push esi
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
push [ebx + IRQH.data]
|
|
|
|
call [ebx + IRQH.handler]
|
2013-05-15 17:10:55 +02:00
|
|
|
pop ecx
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
pop esi
|
|
|
|
pop edi
|
|
|
|
pop ebx
|
|
|
|
|
|
|
|
test eax, eax
|
|
|
|
jz .next
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
inc [ebx + IRQH.num_ints]
|
2020-03-02 05:25:30 +01:00
|
|
|
call reset_irq_active
|
2011-10-14 23:38:50 +02:00
|
|
|
jmp .next
|
|
|
|
|
|
|
|
.done:
|
2020-03-02 05:25:30 +01:00
|
|
|
call reset_irq_active
|
2011-10-14 23:38:50 +02:00
|
|
|
jnc .exit
|
|
|
|
|
2013-05-15 17:10:55 +02:00
|
|
|
; There is at least one configuration with one device which generates IRQ
|
|
|
|
; that is not the same as it should be according to PCI config space.
|
|
|
|
; For that device, the handler is registered at wrong IRQ.
|
|
|
|
; As a workaround, when nobody acknowledges the generated IRQ,
|
|
|
|
; try to ask all other registered handlers; if some handler acknowledges
|
|
|
|
; the IRQ this time, relink it to the current IRQ list.
|
|
|
|
; To make this more reliable, for every handler keep number of times
|
|
|
|
; that it has acknowledged an IRQ, and assume that handlers with at least one
|
|
|
|
; acknowledged IRQ are registered properly.
|
|
|
|
; Note: this still isn't 100% correct, because two IRQs can fire simultaneously,
|
|
|
|
; the better way would be to find the correct IRQ, but I don't know how to do
|
|
|
|
; this in that case.
|
2015-12-30 16:48:01 +01:00
|
|
|
cmp ebp, 1
|
|
|
|
jz .fail
|
2013-05-15 17:10:55 +02:00
|
|
|
push ebp
|
|
|
|
xor ebp, ebp
|
|
|
|
.try_other_irqs:
|
|
|
|
cmp ebp, [esp]
|
|
|
|
jz .try_next_irq
|
2013-05-28 18:29:26 +02:00
|
|
|
cmp ebp, 1
|
|
|
|
jz .try_next_irq
|
2013-07-06 13:33:30 +02:00
|
|
|
cmp ebp, 6
|
|
|
|
jz .try_next_irq
|
2013-05-28 18:29:26 +02:00
|
|
|
cmp ebp, 12
|
|
|
|
jz .try_next_irq
|
2013-07-05 08:43:56 +02:00
|
|
|
cmp ebp, 14
|
|
|
|
jz .try_next_irq
|
|
|
|
cmp ebp, 15
|
|
|
|
jz .try_next_irq
|
2022-02-12 18:27:41 +01:00
|
|
|
lea esi, [irqh_tab + ebp*8]
|
2013-05-15 17:10:55 +02:00
|
|
|
mov ebx, esi
|
|
|
|
.try_next_handler:
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ebx, [ebx + IRQH.list.next]
|
2013-05-15 17:10:55 +02:00
|
|
|
cmp ebx, esi
|
|
|
|
je .try_next_irq
|
2022-02-12 18:27:41 +01:00
|
|
|
cmp [ebx + IRQH.num_ints], 0
|
2013-05-16 21:14:59 +02:00
|
|
|
jne .try_next_handler
|
2013-05-15 20:17:49 +02:00
|
|
|
; keyboard handler acknowledges everything
|
2022-02-12 18:27:41 +01:00
|
|
|
push [ebx + IRQH.data]
|
|
|
|
call [ebx + IRQH.handler]
|
2013-05-15 17:10:55 +02:00
|
|
|
pop ecx
|
|
|
|
test eax, eax
|
2013-05-16 20:45:41 +02:00
|
|
|
jz .try_next_handler
|
|
|
|
|
|
|
|
.found_in_wrong_list:
|
|
|
|
DEBUGF 1,'K : warning: relinking handler from IRQ%d to IRQ%d\n',\
|
|
|
|
ebp, [esp]
|
2013-05-16 23:15:02 +02:00
|
|
|
pop ebp
|
2013-05-16 20:45:41 +02:00
|
|
|
spin_lock_irqsave IrqsList
|
|
|
|
list_del ebx
|
2022-02-12 18:27:41 +01:00
|
|
|
lea edx, [irqh_tab + ebp*8]
|
2013-05-16 20:45:41 +02:00
|
|
|
list_add_tail ebx, edx
|
|
|
|
spin_unlock_irqrestore IrqsList
|
|
|
|
jmp .exit
|
|
|
|
|
2013-05-15 17:10:55 +02:00
|
|
|
.try_next_irq:
|
|
|
|
inc ebp
|
|
|
|
cmp ebp, 16
|
|
|
|
jb .try_other_irqs
|
|
|
|
pop ebp
|
|
|
|
|
2013-05-15 20:17:49 +02:00
|
|
|
.fail:
|
2022-02-12 18:27:41 +01:00
|
|
|
inc [irq_failed + ebp*4]
|
2011-10-14 23:38:50 +02:00
|
|
|
.exit:
|
|
|
|
|
|
|
|
mov ecx, ebp
|
|
|
|
call irq_eoi
|
|
|
|
|
2013-06-07 18:46:52 +02:00
|
|
|
; IRQ handler could make some kernel thread ready; reschedule
|
|
|
|
mov bl, SCHEDULE_HIGHER_PRIORITY
|
|
|
|
call find_next_task
|
|
|
|
jz .return ; if there is only one running process
|
|
|
|
call do_change_task
|
|
|
|
.return:
|
2013-05-15 17:10:55 +02:00
|
|
|
restore_ring3_context
|
2011-10-14 23:38:50 +02:00
|
|
|
add esp, 4
|
|
|
|
iret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
irqD:
|
|
|
|
push eax
|
|
|
|
push ecx
|
|
|
|
xor eax, eax
|
|
|
|
out 0xf0, al
|
|
|
|
mov cl, 13
|
|
|
|
call irq_eoi
|
|
|
|
pop ecx
|
|
|
|
pop eax
|
|
|
|
iret
|
|
|
|
|