Subversion Repositories Kolibri OS

Rev

Rev 2901 | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2012. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;; simple AGP driver for KolibriOS                                 ;;
  7. ;;                                                                 ;;
  8. ;;    Written by hidnplayr@kolibrios.org                           ;;
  9. ;;                                                                 ;;
  10. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  11. ;;             Version 2, June 1991                                ;;
  12. ;;                                                                 ;;
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.  
  15.  
  16. format MS COFF
  17.  
  18. DEBUG                   equ 1
  19. FAST_WRITE              equ 0           ; may cause problems with some motherboards
  20.  
  21. include 'proc32.inc'
  22. include 'imports.inc'
  23.  
  24. struc IOCTL
  25. {  .handle      dd ?
  26.    .io_code     dd ?
  27.    .input       dd ?
  28.    .inp_size    dd ?
  29.    .output      dd ?
  30.    .out_size    dd ?
  31. }
  32.  
  33. virtual at 0
  34.   IOCTL IOCTL
  35. end virtual
  36.  
  37. public START
  38. public service_proc
  39. public version
  40.  
  41. DRV_ENTRY       equ 1
  42. DRV_EXIT        equ -1
  43.  
  44. SRV_GETVERSION  equ 0
  45. SRV_DETECT      equ 1
  46.  
  47. API_VERSION     equ 1
  48.  
  49. section '.flat' code readable align 16
  50.  
  51. proc START stdcall, state:dword
  52.  
  53.         cmp     [state], 1
  54.         jne     .exit
  55. .entry:
  56.  
  57.      if DEBUG
  58.         mov     esi, msgInit
  59.         call    SysMsgBoardStr
  60.      end if
  61.  
  62.         stdcall RegService, my_service, service_proc
  63.         ret
  64. .fail:
  65. .exit:
  66.         xor     eax, eax
  67.         ret
  68. endp
  69.  
  70. handle     equ  IOCTL.handle
  71. io_code    equ  IOCTL.io_code
  72. input      equ  IOCTL.input
  73. inp_size   equ  IOCTL.inp_size
  74. output     equ  IOCTL.output
  75. out_size   equ  IOCTL.out_size
  76.  
  77. align 4
  78. proc service_proc stdcall, ioctl:dword
  79.  
  80.         mov     ebx, [ioctl]
  81.         mov     eax, [ebx+io_code]
  82.         cmp     eax, SRV_GETVERSION
  83.         jne     @F
  84.  
  85.         mov     eax, [ebx+output]
  86.         cmp     [ebx+out_size], 4
  87.         jne     .fail
  88.         mov     [eax], dword API_VERSION
  89.         xor     eax, eax
  90.         ret
  91. @@:
  92.         mov     ebx, [ioctl]
  93.         mov     eax, [ebx+io_code]
  94.         cmp     eax, SRV_DETECT
  95.         jne     @F
  96.         call    detect
  97. @@:
  98. .fail:
  99.         or      eax, -1
  100.         ret
  101. endp
  102.  
  103. restore   handle
  104. restore   io_code
  105. restore   input
  106. restore   inp_size
  107. restore   output
  108. restore   out_size
  109.  
  110. align 4
  111. proc detect
  112.            locals
  113.             last_bus dd ?
  114.            endl
  115.  
  116.         mov     esi, msgSearch
  117.         call    SysMsgBoardStr
  118.  
  119.         xor     eax, eax
  120.         mov     [bus], eax
  121.         inc     eax
  122.         call    PciApi          ; get last bus
  123.         cmp     eax, -1
  124.         je      .error
  125.         mov     [last_bus], eax
  126.  
  127.   .next_bus:
  128.         and     [devfn], 0
  129.   .next_dev:
  130.         stdcall PciRead16, [bus], [devfn], dword 0x0a   ; read class/subclass
  131.  
  132.         cmp     ax, 0x0300      ; display controller - vga compatable controller
  133.         je      .found
  134.  
  135.         cmp     ax, 0x0302      ; display controller - 3d controller
  136.         je      .found
  137.  
  138.         cmp     ax, 0x0380      ; display controller - other display controller
  139.         je      .found
  140.  
  141.   .next:
  142.         inc     [devfn]
  143.         cmp     [devfn], 256
  144.         jb      .next_dev
  145.         mov     eax, [bus]
  146.         inc     eax
  147.         mov     [bus], eax
  148.         cmp     eax, [last_bus]
  149.         jna     .next_bus
  150.  
  151.   .error:
  152.         mov     esi, msgFail
  153.         call    SysMsgBoardStr
  154.  
  155.         xor     eax, eax
  156.         inc     eax
  157.         ret
  158.  
  159.   .found:
  160.         stdcall PciRead8, [bus], [devfn], dword 0x06    ; read prog IF
  161.         test    al, 1 shl 4                             ; got capabilities list?
  162.         jnz     .got_capabilities_list
  163.  
  164.         ; TODO: Do it the old way: detect device and check with a list of known capabilities
  165.         ; stupid pre PCI 2.2 board....
  166.  
  167.         jmp     .next
  168.  
  169.   .got_capabilities_list:
  170.         stdcall PciRead8, [bus], [devfn], dword 0x34    ; read capabilities offset
  171.         and     eax, 11111100b                          ; always dword aligned
  172.         mov     edi, eax
  173.  
  174.   .read_capability:
  175.         stdcall PciRead32, [bus], [devfn], edi          ; read capability
  176.         cmp     al, 0x02                                ; AGP
  177.         je      .got_agp
  178.         movzx   edi, ah                                 ; pointer to next capability
  179.         test    edi, edi
  180.         jnz     .read_capability
  181.         jmp     .next
  182.  
  183.   .got_agp:
  184.         shr     eax, 16
  185.         mov     [revision], al                          ; high nibble = major revision
  186.                                                         ; low nibble = minor revision
  187.         add     edi, 4
  188.         and     al, 0xf0
  189.         cmp     al, 0x30
  190.         je      .agp_3
  191.  
  192.   .agp_2:
  193.         mov     esi, msgAGP2
  194.         call    SysMsgBoardStr
  195.  
  196.         stdcall PciRead32, [bus], [devfn], edi          ; read AGP status
  197.   .agp_2_:
  198.         test    al, 100b
  199.         jnz     .100b
  200.  
  201.         test    al, 10b
  202.         jnz     .010b
  203.  
  204.         test    al, 1b
  205.         jz      .error
  206.  
  207.   .001b:
  208.         mov     [cmd], 001b
  209.         mov     esi, msg1
  210.         call    SysMsgBoardStr
  211.         jmp     .agp_go
  212.  
  213.   .010b:
  214.         mov     [cmd], 010b
  215.         mov     esi, msg2
  216.         call    SysMsgBoardStr
  217.         jmp     .agp_go
  218.  
  219.   .100b:
  220.         mov     [cmd], 100b
  221.         mov     esi, msg4
  222.         call    SysMsgBoardStr
  223.         jmp     .agp_go
  224.  
  225.   .agp_2m:
  226.         mov     esi, msgAGP2m
  227.         call    SysMsgBoardStr
  228.         jmp     .agp_2_
  229.  
  230.   .agp_3:
  231.         mov     esi, msgAGP3
  232.         call    SysMsgBoardStr
  233.  
  234.         stdcall PciRead32, [bus], [devfn], edi          ; read AGP status
  235.         test    al, 1 shl 3
  236.         jz      .agp_2m
  237.  
  238.         test    eax, 10b
  239.         jnz     .8x
  240.         mov     [cmd], 01b
  241.         mov     esi, msg4
  242.         call    SysMsgBoardStr
  243.         jmp     .agp_go
  244.  
  245.   .8x:
  246.         mov     [cmd], 10b
  247.         mov     esi, msg8
  248.         call    SysMsgBoardStr
  249.  
  250.   .agp_go:
  251.  
  252. if FAST_WRITE
  253.         test    ax, 1 shl 4
  254.         jz      @f
  255.         or      [cmd], 1 shl 4
  256.         mov     esi, msgfast
  257.         call    SysMsgBoardStr
  258.   @@:
  259. end if
  260.  
  261.         test    ax, 1 shl 9     ; Side band addressing
  262.         jz      @f
  263.         or      [cmd], 1 shl 9
  264.         mov     esi, msgside
  265.         call    SysMsgBoardStr
  266.   @@:
  267.  
  268.         add     edi, 4
  269.         mov     eax, [cmd]
  270.         or      eax, 1 shl 8                            ; enable AGP
  271.         stdcall PciWrite32, [bus], [devfn], edi, eax    ; write AGP cmd
  272.  
  273.         mov     esi, msgOK
  274.         call    SysMsgBoardStr
  275.  
  276.         ret
  277.  
  278. endp
  279.  
  280.  
  281. ; initialized data
  282.  
  283. align 4
  284. version         dd (5 shl 16) or (API_VERSION and 0xFFFF)
  285.  
  286. my_service      db 'AGP', 0                             ; max 16 chars include zero
  287.  
  288. msgInit         db 'AGP driver loaded.', 13, 10, 0
  289. msgSearch       db 'Searching for AGP card...', 13, 10, 0
  290. msgFail         db 'device not found', 13, 10, 0
  291. msgOK           db 'AGP device enabled', 13, 10, 0
  292. msgAGP2         db 'AGP2 device found', 13, 10, 0
  293. msgAGP3         db 'AGP3 device found', 13, 10, 0
  294. msgAGP2m        db 'Running in AGP2 mode', 13, 10, 0
  295. msg8            db '8x speed', 13, 10, 0
  296. msg4            db '4x speed', 13, 10, 0
  297. msg2            db '2x speed', 13, 10, 0
  298. msg1            db '1x speed', 13, 10, 0
  299. msgfast         db 'Fast Write', 13, 10, 0
  300. msgside         db 'Side band addressing', 13, 10, 0
  301.  
  302. section '.data' data readable writable align 16
  303.  
  304. ; uninitialized data
  305.  
  306. revision        db ?
  307. cmd             dd ?
  308. bus             dd ?
  309. devfn           dd ?
  310.  
  311.