Subversion Repositories Kolibri OS

Rev

Rev 6283 | Rev 6320 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <syscall.h>
  2.  
  3. #include <drm/drmP.h>
  4. #include <drm/i915_drm.h>
  5. #include "i915_drv.h"
  6.  
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/mod_devicetable.h>
  10. #include <linux/pci.h>
  11.  
  12. #include "getopt.h"
  13.  
  14. #include "bitmap.h"
  15. #include "i915_kos32.h"
  16.  
  17. #define DRV_NAME "i915 v4.4.3"
  18.  
  19. #define I915_DEV_CLOSE 0
  20. #define I915_DEV_INIT  1
  21. #define I915_DEV_READY 2
  22.  
  23. static int my_atoi(char **cmd);
  24. static char* parse_mode(char *p, videomode_t *mode);
  25. void cpu_detect1();
  26. int kmap_init();
  27.  
  28. unsigned long volatile jiffies;
  29. int oops_in_progress;
  30. int x86_clflush_size;
  31. unsigned int tsc_khz;
  32. struct workqueue_struct *system_wq;
  33. int driver_wq_state;
  34. struct drm_device *main_device;
  35. struct drm_file   *drm_file_handlers[256];
  36. videomode_t usermode;
  37. extern int __getopt_initialized;
  38.  
  39. void i915_driver_thread()
  40. {
  41.     struct drm_i915_private *dev_priv = NULL;
  42.     struct workqueue_struct *cwq = NULL;
  43.     static int dpms = 1;
  44.     static int dpms_lock = 0;
  45.     oskey_t   key;
  46.     unsigned long irqflags;
  47.     int tmp;
  48.  
  49.     printf("%s\n",__FUNCTION__);
  50.  
  51.     while(driver_wq_state == I915_DEV_INIT)
  52.     {
  53.         jiffies = GetClockNs() / 10000000;
  54.         delay(1);
  55.     };
  56.  
  57.     if( driver_wq_state == I915_DEV_CLOSE)
  58.     {
  59.         asm volatile ("int $0x40"::"a"(-1));
  60.     };
  61.  
  62.     dev_priv = main_device->dev_private;
  63.  
  64.     asm volatile("int $0x40":"=a"(tmp):"a"(66),"b"(1),"c"(1));
  65.     asm volatile("int $0x40":"=a"(tmp):"a"(66),"b"(4),"c"(0x46),"d"(0x330));
  66.     asm volatile("int $0x40":"=a"(tmp):"a"(66),"b"(4),"c"(0xC6),"d"(0x330));
  67.  
  68.     while(driver_wq_state != I915_DEV_CLOSE)
  69.     {
  70.         jiffies = GetClockNs() / 10000000;
  71.  
  72.         key = get_key();
  73.  
  74.         if( (key.val != 1) && (key.state == 0x02))
  75.         {
  76.             if(key.code == 0x46 && dpms_lock == 0)
  77.             {
  78.                 dpms_lock = 1;
  79.                 if(dpms == 1)
  80.                 {
  81.                     i915_dpms(main_device, DRM_MODE_DPMS_OFF);
  82.                     printf("dpms off\n");
  83.                 }
  84.                 else
  85.                 {
  86.                     i915_dpms(main_device, DRM_MODE_DPMS_ON);
  87.                     printf("dpms on\n");
  88.                 };
  89.                 dpms ^= 1;
  90.                 }
  91.             else if(key.code == 0xC6)
  92.                 dpms_lock = 0;
  93.         };
  94.         cwq = dev_priv->wq;
  95.  
  96.         spin_lock_irqsave(&cwq->lock, irqflags);
  97.         while (!list_empty(&cwq->worklist))
  98.         {
  99.             struct work_struct *work = list_entry(cwq->worklist.next,
  100.                                         struct work_struct, entry);
  101.             work_func_t f = work->func;
  102.             list_del_init(cwq->worklist.next);
  103.  
  104.             spin_unlock_irqrestore(&cwq->lock, irqflags);
  105.             f(work);
  106.             spin_lock_irqsave(&cwq->lock, irqflags);
  107.         }
  108.         spin_unlock_irqrestore(&cwq->lock, irqflags);
  109.  
  110.         cwq = dev_priv->hotplug.dp_wq;
  111.  
  112.         spin_lock_irqsave(&cwq->lock, irqflags);
  113.         while (!list_empty(&cwq->worklist))
  114.         {
  115.             struct work_struct *work = list_entry(cwq->worklist.next,
  116.                                         struct work_struct, entry);
  117.             work_func_t f = work->func;
  118.             list_del_init(cwq->worklist.next);
  119.  
  120.             spin_unlock_irqrestore(&cwq->lock, irqflags);
  121.             f(work);
  122.             spin_lock_irqsave(&cwq->lock, irqflags);
  123.         }
  124.         spin_unlock_irqrestore(&cwq->lock, irqflags);
  125.  
  126.         delay(1);
  127.     };
  128.  
  129.     asm volatile ("int $0x40"::"a"(-1));
  130. }
  131.  
  132. u32  __attribute__((externally_visible)) drvEntry(int action, char *cmdline)
  133. {
  134.     static pci_dev_t device;
  135.     const struct pci_device_id  *ent;
  136.     char *safecmdline;
  137.     int err = 0;
  138.  
  139.     if(action != 1)
  140.     {
  141.         driver_wq_state = I915_DEV_CLOSE;
  142.         return 0;
  143.     };
  144.  
  145.     if( GetService("DISPLAY") != 0 )
  146.         return 0;
  147.  
  148.     printf("\n%s build %s %s\nusage: i915 [options]\n",
  149.            DRV_NAME, __DATE__, __TIME__);
  150.  
  151.     printf("--rc6 <-1,0-7>  Enable power-saving render C-state 6.\n"
  152.            "                Different stages can be selected via bitmask values\n"
  153.            "                (0 = disable; 1 = enable rc6; 2 = enable deep rc6;\n"
  154.            "                4 = enable deepest rc6).\n"
  155.            "                For example, 3 would enable rc6 and deep rc6,\n"
  156.            "                and 7 would enable everything.\n"
  157.            "                default: -1 (use per-chip default)\n");
  158.     printf("--fbc <-1,0,1>  Enable frame buffer compression for power savings\n"
  159.            "                (default: -1 (use per-chip default))\n");
  160.     printf("-l\n"
  161.            "--log  <path>   path to log file\n");
  162.     printf("-m\n"
  163.            "--mode <WxHxHz> set videomode\n");
  164.     printf("-v\n"
  165.            "--video <CONNECTOR>:<xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]\n"
  166.            "                set videomode for CONNECTOR\n");
  167.  
  168.     if( cmdline && *cmdline )
  169.     {
  170.         int argc, i, c;
  171.         char **argv;
  172.  
  173.         safecmdline = __builtin_strdup(cmdline);
  174.         printf("cmdline %s\n", safecmdline);
  175.  
  176.         argc = split_cmdline(safecmdline, NULL);
  177.         argv = __builtin_malloc((argc+1)*sizeof(char*));
  178.         split_cmdline(safecmdline, argv);
  179.         argv[argc] = NULL;
  180.  
  181.         while(1)
  182.         {
  183.             static struct option long_options[] =
  184.             {
  185.                 {"log",   required_argument, 0, 'l'},
  186.                 {"mode",  required_argument, 0, 'm'},
  187.                 {"video", required_argument, 0, 'v'},
  188.                 {"rc6", required_argument, 0, OPTION_RC6},
  189.                 {"fbc", required_argument, 0, OPTION_FBC},
  190.                 {0, 0, 0, 0}
  191.             };
  192.  
  193.             int option_index = 0;
  194.  
  195.             c = getopt_long (argc, argv, "l:m:v:",
  196.                             long_options, &option_index);
  197.  
  198.             if (c == -1)
  199.                 break;
  200.  
  201.             switch(c)
  202.             {
  203.                 case OPTION_RC6:
  204.                     i915.enable_rc6 = my_atoi(&optarg);
  205.                     printf("i915.rc6 = %d\n",i915.enable_rc6);
  206.                     break;
  207.  
  208.                 case OPTION_FBC:
  209.                     i915.enable_fbc = my_atoi(&optarg);
  210.                     printf("i915.fbc = %d\n",i915.enable_fbc);
  211.                     break;
  212.  
  213.                 case 'l':
  214.                     i915.log_file = optarg;
  215.                     break;
  216.  
  217.                 case 'm':
  218.                     parse_mode(optarg, &usermode);
  219.                     break;
  220.  
  221.                 case 'v':
  222.                     i915.cmdline_mode = optarg;
  223.                     printf("i915.cmdline_mode =%s\n",i915.cmdline_mode);
  224.                     break;
  225.             }
  226.         }
  227.     };
  228.  
  229.     if( i915.log_file && !dbg_open(i915.log_file))
  230.     {
  231.         printf("Can't open %s\nExit\n", i915.log_file);
  232.         return 0;
  233.     }
  234.     else
  235.     {
  236.         dbgprintf("\nLOG: %s build %s %s\n",DRV_NAME,__DATE__, __TIME__);
  237.     }
  238.  
  239.     cpu_detect1();
  240.  
  241.     err = enum_pci_devices();
  242.     if( unlikely(err != 0) )
  243.     {
  244.         dbgprintf("Device enumeration failed\n");
  245.         return 0;
  246.     }
  247.  
  248.     err = kmap_init();
  249.     if( unlikely(err != 0) )
  250.     {
  251.         dbgprintf("kmap initialization failed\n");
  252.         return 0;
  253.     }
  254.  
  255.     dmi_scan_machine();
  256.  
  257.     driver_wq_state = I915_DEV_INIT;
  258.     CreateKernelThread(i915_driver_thread);
  259.  
  260.     err = i915_init();
  261.     if(unlikely(err!= 0))
  262.     {
  263.         driver_wq_state = I915_DEV_CLOSE;
  264.         dbgprintf("Epic Fail :(\n");
  265.         delay(100);
  266.         return 0;
  267.     };
  268.  
  269.     driver_wq_state = I915_DEV_READY;
  270.  
  271.     init_display_kms(main_device, &usermode);
  272.  
  273.     err = RegService("DISPLAY", display_handler);
  274.  
  275.     if( err != 0)
  276.         dbgprintf("Set DISPLAY handler\n");
  277.  
  278.     return err;
  279. };
  280.  
  281. int do_command_line(const char* usercmd)
  282. {
  283.     char *cmdline;
  284.     int argc, i, c;
  285.     char **argv;
  286.     int retval = 0;
  287.  
  288.     if( (usercmd == NULL) || (*usercmd == 0) )
  289.         return 1;
  290.  
  291.     cmdline = __builtin_strdup(usercmd);
  292.     printf("cmdline %s\n", cmdline);
  293.  
  294.     argc = split_cmdline(cmdline, NULL);
  295.     argv = __builtin_malloc((argc+1)*sizeof(char*));
  296.     split_cmdline(cmdline, argv);
  297.     argv[argc] = NULL;
  298.  
  299.     __getopt_initialized = 0;
  300.  
  301.     while(1)
  302.     {
  303.         static struct option long_options[] =
  304.         {
  305.             {"list-connectors",      no_argument,       0, OPTION_CONNECTORS},
  306.             {"list-connector-modes", required_argument, 0, OPTION_CONN_MODES},
  307.             {"video",                required_argument, 0, 'v'},
  308.             {0, 0, 0, 0}
  309.         };
  310.  
  311.         int option_index = 0;
  312.  
  313.         c = getopt_long (argc, argv, "v:",
  314.                         long_options, &option_index);
  315.  
  316.         if (c == -1)
  317.             break;
  318.  
  319.         switch(c)
  320.         {
  321.             case 'v':
  322.                 printf("cmdline_mode %s\n",optarg);
  323.                 retval = set_cmdline_mode_ext(main_device, optarg);
  324.                 break;
  325.  
  326.             case OPTION_CONNECTORS:
  327.                 list_connectors(main_device);
  328.                 break;
  329.  
  330.             case OPTION_CONN_MODES:
  331.                 retval = list_connector_modes(main_device, optarg);
  332.                 break;
  333.         }
  334.     }
  335.     __builtin_free(argv);
  336.     __builtin_free(cmdline);
  337.  
  338.     return retval;
  339. };
  340.  
  341. #define CURRENT_API     0x0200      /*      2.00     */
  342. #define COMPATIBLE_API  0x0100      /*      1.00     */
  343.  
  344. #define API_VERSION     (COMPATIBLE_API << 16) | CURRENT_API
  345. #define DISPLAY_VERSION  API_VERSION
  346.  
  347.  
  348. #define SRV_GETVERSION              0
  349. #define SRV_ENUM_MODES              1
  350. #define SRV_SET_MODE                2
  351. #define SRV_GET_CAPS                3
  352. #define SRV_CMDLINE                 4
  353.  
  354. #define SRV_GET_PCI_INFO                20
  355. #define SRV_I915_GET_PARAM              21
  356. #define SRV_I915_GEM_CREATE             22
  357. #define SRV_DRM_GEM_CLOSE               23
  358. #define SRV_DRM_GEM_FLINK               24
  359. #define SRV_DRM_GEM_OPEN                25
  360. #define SRV_I915_GEM_PIN                26
  361. #define SRV_I915_GEM_UNPIN              27
  362. #define SRV_I915_GEM_GET_CACHING        28
  363. #define SRV_I915_GEM_SET_CACHING        29
  364. #define SRV_I915_GEM_PWRITE             30
  365. #define SRV_I915_GEM_BUSY               31
  366. #define SRV_I915_GEM_SET_DOMAIN         32
  367. #define SRV_I915_GEM_MMAP               33
  368. #define SRV_I915_GEM_SET_TILING         34
  369. #define SRV_I915_GEM_GET_TILING         35
  370. #define SRV_I915_GEM_GET_APERTURE       36
  371. #define SRV_I915_GEM_MMAP_GTT           37
  372. #define SRV_I915_GEM_THROTTLE           38
  373. #define SRV_I915_GEM_EXECBUFFER2        39
  374. #define SRV_I915_GEM_WAIT               40
  375. #define SRV_I915_GEM_CONTEXT_CREATE     41
  376. #define SRV_I915_GEM_CONTEXT_DESTROY    42
  377. #define SRV_I915_REG_READ               43
  378.  
  379. #define SRV_FBINFO                      44
  380. #define SRV_MASK_UPDATE                 45
  381. #define SRV_MASK_UPDATE_EX              46
  382.  
  383. #define SRV_I915_GEM_PREAD              47
  384.  
  385. #define check_input(size) \
  386.     if( unlikely((inp==NULL)||(io->inp_size != (size))) )   \
  387.         break;
  388.  
  389. #define check_output(size) \
  390.     if( unlikely((outp==NULL)||(io->out_size != (size))) )   \
  391.         break;
  392.  
  393. int _stdcall display_handler(ioctl_t *io)
  394. {
  395.     struct drm_file *file;
  396.  
  397.     int  retval = -1;
  398.     u32 *inp;
  399.     u32 *outp;
  400.  
  401.     inp = io->input;
  402.     outp = io->output;
  403.  
  404.     file = drm_file_handlers[0];
  405.  
  406.     switch(io->io_code)
  407.     {
  408.         case SRV_GETVERSION:
  409.             check_output(4);
  410.             *outp  = DISPLAY_VERSION;
  411.             retval = 0;
  412.             break;
  413.  
  414.         case SRV_ENUM_MODES:
  415. //            dbgprintf("SRV_ENUM_MODES inp %x inp_size %x out_size %x\n",
  416. //                       inp, io->inp_size, io->out_size );
  417.             check_output(4);
  418. //            check_input(*outp * sizeof(videomode_t));
  419.             retval = get_videomodes((videomode_t*)inp, outp);
  420.             break;
  421.  
  422.         case SRV_SET_MODE:
  423. //            dbgprintf("SRV_SET_MODE inp %x inp_size %x\n",
  424. //                       inp, io->inp_size);
  425.             check_input(sizeof(videomode_t));
  426.             retval = set_user_mode((videomode_t*)inp);
  427.             break;
  428.  
  429.         case SRV_GET_CAPS:
  430.             retval = get_driver_caps((hwcaps_t*)inp);
  431.             break;
  432.  
  433.         case SRV_CMDLINE:
  434.             retval = do_command_line((char*)inp);
  435.             break;
  436.  
  437.         case SRV_GET_PCI_INFO:
  438.             get_pci_info((struct pci_device *)inp);
  439.             retval = 0;
  440.             break;
  441.  
  442.         case SRV_I915_GET_PARAM:
  443.             retval = i915_getparam(main_device, inp, file);
  444.             break;
  445.  
  446.         case SRV_I915_GEM_CREATE:
  447.             retval = i915_gem_create_ioctl(main_device, inp, file);
  448.             break;
  449.  
  450.         case SRV_DRM_GEM_CLOSE:
  451.             retval = drm_gem_close_ioctl(main_device, inp, file);
  452.             break;
  453.  
  454.         case SRV_DRM_GEM_FLINK:
  455.             retval = drm_gem_flink_ioctl(main_device, inp, file);
  456.             break;
  457.  
  458.         case SRV_DRM_GEM_OPEN:
  459.             retval = drm_gem_open_ioctl(main_device, inp, file);
  460.             break;
  461.  
  462.         case SRV_I915_GEM_GET_CACHING:
  463.             retval = i915_gem_get_caching_ioctl(main_device, inp, file);
  464.             break;
  465.  
  466.         case SRV_I915_GEM_SET_CACHING:
  467.             retval = i915_gem_set_caching_ioctl(main_device, inp, file);
  468.             break;
  469.  
  470.         case SRV_I915_GEM_PREAD:
  471.             retval = i915_gem_pread_ioctl(main_device, inp, file);
  472.             break;
  473.  
  474.         case SRV_I915_GEM_PWRITE:
  475.             retval = i915_gem_pwrite_ioctl(main_device, inp, file);
  476.             break;
  477.  
  478.         case SRV_I915_GEM_BUSY:
  479.             retval = i915_gem_busy_ioctl(main_device, inp, file);
  480.             break;
  481.  
  482.         case SRV_I915_GEM_SET_DOMAIN:
  483.             retval = i915_gem_set_domain_ioctl(main_device, inp, file);
  484.             break;
  485.  
  486.         case SRV_I915_GEM_MMAP:
  487.             retval = i915_gem_mmap_ioctl(main_device, inp, file);
  488.             break;
  489.  
  490.         case SRV_I915_GEM_SET_TILING:
  491.             retval = i915_gem_set_tiling(main_device, inp, file);
  492.             break;
  493.  
  494.         case SRV_I915_GEM_GET_TILING:
  495.             retval = i915_gem_get_tiling(main_device, inp, file);
  496.             break;
  497.  
  498.         case SRV_I915_GEM_GET_APERTURE:
  499. //            printf("SRV_I915_GEM_GET_APERTURE ");
  500.             retval = i915_gem_get_aperture_ioctl(main_device, inp, file);
  501. //            printf(" retval=%d\n", retval);
  502.             break;
  503.  
  504.         case SRV_I915_GEM_MMAP_GTT:
  505.             retval = i915_gem_mmap_gtt_ioctl(main_device, inp, file);
  506.             break;
  507.  
  508.         case SRV_I915_GEM_THROTTLE:
  509.             retval = i915_gem_throttle_ioctl(main_device, inp, file);
  510.             break;
  511.  
  512.         case SRV_I915_GEM_EXECBUFFER2:
  513.             retval = i915_gem_execbuffer2(main_device, inp, file);
  514.             break;
  515.  
  516.         case SRV_I915_GEM_WAIT:
  517.             retval = i915_gem_wait_ioctl(main_device, inp, file);
  518.             break;
  519.  
  520.         case SRV_I915_GEM_CONTEXT_CREATE:
  521.             retval = i915_gem_context_create_ioctl(main_device, inp, file);
  522.             break;
  523.  
  524.         case SRV_I915_GEM_CONTEXT_DESTROY:
  525.             retval = i915_gem_context_destroy_ioctl(main_device, inp, file);
  526.             break;
  527.  
  528.         case SRV_I915_REG_READ:
  529.             retval = i915_reg_read_ioctl(main_device, inp, file);
  530.             break;
  531.  
  532.         case SRV_FBINFO:
  533.             retval = i915_fbinfo(inp);
  534.             break;
  535.  
  536.         case SRV_MASK_UPDATE:
  537.             retval = i915_mask_update(main_device, inp, file);
  538.             break;
  539.  
  540.         case SRV_MASK_UPDATE_EX:
  541.             retval = i915_mask_update_ex(main_device, inp, file);
  542.             break;
  543.     };
  544.  
  545.     return retval;
  546. }
  547.  
  548.  
  549. #define PCI_CLASS_REVISION      0x08
  550. #define PCI_CLASS_DISPLAY_VGA   0x0300
  551. #define PCI_CLASS_BRIDGE_HOST   0x0600
  552. #define PCI_CLASS_BRIDGE_ISA    0x0601
  553.  
  554. int pci_scan_filter(u32 id, u32 busnr, u32 devfn)
  555. {
  556.     u16 vendor, device;
  557.     u32 class;
  558.     int   ret = 0;
  559.  
  560.     vendor   = id & 0xffff;
  561.     device   = (id >> 16) & 0xffff;
  562.  
  563.     if(vendor == 0x8086)
  564.     {
  565.         class = PciRead32(busnr, devfn, PCI_CLASS_REVISION);
  566.         class >>= 16;
  567.  
  568.         if( (class == PCI_CLASS_DISPLAY_VGA) ||
  569.             (class == PCI_CLASS_BRIDGE_HOST) ||
  570.             (class == PCI_CLASS_BRIDGE_ISA))
  571.             ret = 1;
  572.     }
  573.     return ret;
  574. };
  575.  
  576.  
  577. struct mtrr
  578. {
  579.     u64  base;
  580.     u64  mask;
  581. };
  582.  
  583. struct cpuinfo
  584. {
  585.     u64  caps;
  586.     u64  def_mtrr;
  587.     u64  mtrr_cap;
  588.     int    var_mtrr_count;
  589.     int    fix_mtrr_count;
  590.     struct mtrr var_mtrr[9];
  591.     char   model_name[64];
  592. };
  593.  
  594. #define MTRRphysBase_MSR(reg) (0x200 + 2 * (reg))
  595. #define MTRRphysMask_MSR(reg) (0x200 + 2 * (reg) + 1)
  596.  
  597. #define MSR_MTRRdefType                 0x000002ff
  598.  
  599. #define IA32_MTRRCAP            0xFE
  600. #define IA32_CR_PAT_MSR         0x277
  601.  
  602. #define PAT_TYPE_UC             0
  603. #define PAT_TYPE_WC             1
  604. #define PAT_TYPE_WB             6
  605. #define PAT_TYPE_UCM            7
  606.  
  607.  
  608. #define MTRR_UC                 0
  609. #define MTRR_WC                 1
  610. #define MTRR_WB                 6
  611.  
  612. static inline u64 read_msr(u32 msr)
  613. {
  614.     union {
  615.         u64  val;
  616.         struct {
  617.             u32 low;
  618.             u32 high;
  619.         };
  620.     }tmp;
  621.  
  622.     asm volatile (
  623.     "rdmsr"
  624.     : "=a" (tmp.low), "=d" (tmp.high)
  625.     : "c" (msr));
  626.     return tmp.val;
  627. }
  628.  
  629. static inline void write_msr(u32 msr, u64 val)
  630. {
  631.     union {
  632.         u64  val;
  633.         struct {
  634.             u32 low;
  635.             u32 high;
  636.         };
  637.     }tmp;
  638.  
  639.     tmp.val = val;
  640.  
  641.     asm volatile (
  642.     "wrmsr"
  643.     :: "a" (tmp.low), "d" (tmp.high), "c" (msr));
  644. }
  645.  
  646. #define SIZE_OR_MASK_BITS(n)  (~((1ULL << ((n) - PAGE_SHIFT)) - 1))
  647.  
  648. static void set_mtrr(unsigned int reg, unsigned long base,
  649.                  unsigned long size, int type)
  650. {
  651.     unsigned int base_lo, base_hi, mask_lo, mask_hi;
  652.     u64 size_or_mask, size_and_mask;
  653.  
  654.     size_or_mask = SIZE_OR_MASK_BITS(36);
  655.     size_and_mask = 0x00f00000;
  656.  
  657.     if (size == 0) {
  658.         /*
  659.          * The invalid bit is kept in the mask, so we simply
  660.          * clear the relevant mask register to disable a range.
  661.          */
  662.         native_write_msr(MTRRphysMask_MSR(reg), 0, 0);
  663.     }
  664.     else {
  665.         base_lo = base << PAGE_SHIFT | type;
  666.         base_hi = (base & size_and_mask) >> (32 - PAGE_SHIFT);
  667.         mask_lo = -size << PAGE_SHIFT | 0x800;
  668.         mask_hi = (-size & size_and_mask) >> (32 - PAGE_SHIFT);
  669.  
  670.         native_write_msr(MTRRphysBase_MSR(reg), base_lo, base_hi);
  671.         native_write_msr(MTRRphysMask_MSR(reg), mask_lo, mask_hi);
  672.     };
  673. }
  674.  
  675.  
  676. static u32 deftype_lo, deftype_hi;
  677.  
  678. void cpu_detect1()
  679. {
  680.     struct cpuinfo cpuinfo;
  681.  
  682.     u32 junk, tfms, cap0, misc;
  683.     int i;
  684.  
  685.     cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  686.  
  687.     if (cap0 & (1<<19))
  688.     {
  689.         x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  690.     }
  691.  
  692. #if 0
  693.     cpuid(0x80000002, (unsigned int*)&cpuinfo.model_name[0], (unsigned int*)&cpuinfo.model_name[4],
  694.           (unsigned int*)&cpuinfo.model_name[8], (unsigned int*)&cpuinfo.model_name[12]);
  695.     cpuid(0x80000003, (unsigned int*)&cpuinfo.model_name[16], (unsigned int*)&cpuinfo.model_name[20],
  696.           (unsigned int*)&cpuinfo.model_name[24], (unsigned int*)&cpuinfo.model_name[28]);
  697.     cpuid(0x80000004, (unsigned int*)&cpuinfo.model_name[32], (unsigned int*)&cpuinfo.model_name[36],
  698.           (unsigned int*)&cpuinfo.model_name[40], (unsigned int*)&cpuinfo.model_name[44]);
  699.  
  700.     printf("\n%s\n\n",cpuinfo.model_name);
  701.  
  702.     cpuinfo.def_mtrr = read_msr(MSR_MTRRdefType);
  703.     cpuinfo.mtrr_cap = read_msr(IA32_MTRRCAP);
  704.  
  705.     printf("MSR_MTRRdefType %016llx\n\n", cpuinfo.def_mtrr);
  706.  
  707.     cpuinfo.var_mtrr_count = (u8_t)cpuinfo.mtrr_cap;
  708.  
  709.     for(i = 0; i < cpuinfo.var_mtrr_count; i++)
  710.     {
  711.         u64_t mtrr_base;
  712.         u64_t mtrr_mask;
  713.  
  714.         cpuinfo.var_mtrr[i].base = read_msr(MTRRphysBase_MSR(i));
  715.         cpuinfo.var_mtrr[i].mask = read_msr(MTRRphysMask_MSR(i));
  716.  
  717.         printf("MTRR_%d base: %016llx mask: %016llx\n", i,
  718.                cpuinfo.var_mtrr[i].base,
  719.                cpuinfo.var_mtrr[i].mask);
  720.     };
  721.  
  722.     unsigned int cr0, cr3, cr4, eflags;
  723.  
  724.     eflags = safe_cli();
  725.  
  726.     /* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */
  727.     cr0 = read_cr0() | (1<<30);
  728.     write_cr0(cr0);
  729.     wbinvd();
  730.  
  731.     cr4 = read_cr4();
  732.     write_cr4(cr4 & ~(1<<7));
  733.  
  734.     cr3 = read_cr3();
  735.     write_cr3(cr3);
  736.  
  737.     /* Save MTRR state */
  738.     rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
  739.  
  740.     /* Disable MTRRs, and set the default type to uncached */
  741.     native_write_msr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi);
  742.     wbinvd();
  743.  
  744.     i = 0;
  745.     set_mtrr(i++,0,0x80000000>>12,MTRR_WB);
  746.     set_mtrr(i++,0x80000000>>12,0x40000000>>12,MTRR_WB);
  747.     set_mtrr(i++,0xC0000000>>12,0x20000000>>12,MTRR_WB);
  748.     set_mtrr(i++,0xdb800000>>12,0x00800000>>12,MTRR_UC);
  749.     set_mtrr(i++,0xdc000000>>12,0x04000000>>12,MTRR_UC);
  750.     set_mtrr(i++,0xE0000000>>12,0x10000000>>12,MTRR_WC);
  751.  
  752.     for(; i < cpuinfo.var_mtrr_count; i++)
  753.         set_mtrr(i,0,0,0);
  754.  
  755.     write_cr3(cr3);
  756.  
  757.     /* Intel (P6) standard MTRRs */
  758.     native_write_msr(MSR_MTRRdefType, deftype_lo, deftype_hi);
  759.  
  760.     /* Enable caches */
  761.     write_cr0(read_cr0() & ~(1<<30));
  762.  
  763.     /* Restore value of CR4 */
  764.     write_cr4(cr4);
  765.  
  766.     safe_sti(eflags);
  767.  
  768.     printf("\nnew MTRR map\n\n");
  769.  
  770.     for(i = 0; i < cpuinfo.var_mtrr_count; i++)
  771.     {
  772.         u64_t mtrr_base;
  773.         u64_t mtrr_mask;
  774.  
  775.         cpuinfo.var_mtrr[i].base = read_msr(MTRRphysBase_MSR(i));
  776.         cpuinfo.var_mtrr[i].mask = read_msr(MTRRphysMask_MSR(i));
  777.  
  778.         printf("MTRR_%d base: %016llx mask: %016llx\n", i,
  779.                cpuinfo.var_mtrr[i].base,
  780.                cpuinfo.var_mtrr[i].mask);
  781.     };
  782. #endif
  783.  
  784.     tsc_khz = (unsigned int)(GetCpuFreq()/1000);
  785. }
  786.  
  787.  
  788. int get_driver_caps(hwcaps_t *caps)
  789. {
  790.     int ret = 0;
  791.  
  792.     switch(caps->idx)
  793.     {
  794.         case 0:
  795.             caps->opt[0] = 0;
  796.             caps->opt[1] = 0;
  797.             break;
  798.  
  799.         case 1:
  800.             caps->cap1.max_tex_width  = 4096;
  801.             caps->cap1.max_tex_height = 4096;
  802.             break;
  803.         default:
  804.             ret = 1;
  805.     };
  806.     caps->idx = 1;
  807.     return ret;
  808. }
  809.  
  810.  
  811. void get_pci_info(struct pci_device *dev)
  812. {
  813.     struct pci_dev *pdev = main_device->pdev;
  814.  
  815.     memset(dev, sizeof(*dev), 0);
  816.  
  817.     dev->domain     = 0;
  818.     dev->bus        = pdev->busnr;
  819.     dev->dev        = pdev->devfn >> 3;
  820.     dev->func       = pdev->devfn & 7;
  821.     dev->vendor_id  = pdev->vendor;
  822.     dev->device_id  = pdev->device;
  823.     dev->revision   = pdev->revision;
  824. };
  825.  
  826.  
  827.  
  828. char *strstr(const char *cs, const char *ct);
  829.  
  830. static int my_atoi(char **cmd)
  831. {
  832.     char* p = *cmd;
  833.     int val = 0;
  834.     int sign = 1;
  835.  
  836.     if(*p == '-')
  837.     {
  838.         sign = -1;
  839.         p++;
  840.     };
  841.  
  842.     for (;; *p++) {
  843.         switch (*p) {
  844.         case '0' ... '9':
  845.             val = 10*val+(*p-'0');
  846.             break;
  847.         default:
  848.             *cmd = p;
  849.             return val*sign;
  850.         }
  851.     }
  852. }
  853.  
  854. static char* parse_mode(char *p, videomode_t *mode)
  855. {
  856.     char c;
  857.  
  858.     while( (c = *p++) == ' ');
  859.  
  860.     if( c )
  861.     {
  862.         p--;
  863.  
  864.         mode->width = my_atoi(&p);
  865.         if(*p == 'x') p++;
  866.  
  867.         mode->height = my_atoi(&p);
  868.         if(*p == 'x') p++;
  869.  
  870.         mode->bpp = 32;
  871.  
  872.         mode->freq = my_atoi(&p);
  873.  
  874.         if( mode->freq == 0 )
  875.             mode->freq = 60;
  876.     }
  877.  
  878.     return p;
  879. };
  880.  
  881.