Subversion Repositories Kolibri OS

Rev

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