Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. #include <drm/drmP.h>
  3. #include <drm/i915_drm.h>
  4. #include "i915_drv.h"
  5. #include "intel_drv.h"
  6. #include "hmm.h"
  7. #include "bitmap.h"
  8.  
  9. #define DRIVER_CAPS_0   HW_BIT_BLIT;
  10. #define DRIVER_CAPS_1   0
  11.  
  12. struct context *context_map[256];
  13.  
  14. struct hmm bm_mm;
  15.  
  16. extern struct drm_device *main_device;
  17.  
  18.  
  19. void __attribute__((regparm(1))) destroy_bitmap(bitmap_t *bitmap)
  20. {
  21.     dma_addr_t *pages = bitmap->obj->allocated_pages;
  22.     int i;
  23.  
  24.     free_handle(&bm_mm, bitmap->handle);
  25.     bitmap->handle = 0;
  26.     bitmap->obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  27.     bitmap->obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  28.  
  29.     mutex_lock(&main_device->struct_mutex);
  30.  
  31.     drm_gem_object_unreference(&bitmap->obj->base);
  32.     mutex_unlock(&main_device->struct_mutex);
  33.  
  34.     if(pages != NULL)
  35.     {
  36.         for (i = 0; i < bitmap->page_count; i++)
  37.             FreePage(pages[i]);
  38.  
  39.         DRM_DEBUG("%s freec %d pages\n", __FUNCTION__, bitmap->page_count);
  40.  
  41.         free(pages);
  42.     };
  43.     UserFree(bitmap->uaddr);
  44.     __DestroyObject(bitmap);
  45. };
  46.  
  47.  
  48. static int bitmap_get_pages_gtt(struct drm_i915_gem_object *obj)
  49. {
  50.     int page_count;
  51.  
  52.     /* Get the list of pages out of our struct file.  They'll be pinned
  53.      * at this point until we release them.
  54.      */
  55.     page_count = obj->base.size / PAGE_SIZE;
  56.     BUG_ON(obj->allocated_pages == NULL);
  57.     BUG_ON(obj->pages.page != NULL);
  58.  
  59.     obj->pages.page = obj->allocated_pages;
  60.     obj->pages.nents = page_count;
  61.  
  62.  
  63. //   if (obj->tiling_mode != I915_TILING_NONE)
  64. //       i915_gem_object_do_bit_17_swizzle(obj);
  65.  
  66.     return 0;
  67. }
  68.  
  69. static void bitmap_put_pages_gtt(struct drm_i915_gem_object *obj)
  70. {
  71.     int ret, i;
  72.  
  73.     BUG_ON(obj->madv == __I915_MADV_PURGED);
  74.  
  75.     ret = i915_gem_object_set_to_cpu_domain(obj, true);
  76.     if (ret) {
  77.         /* In the event of a disaster, abandon all caches and
  78.          * hope for the best.
  79.          */
  80.         WARN_ON(ret != -EIO);
  81.         i915_gem_clflush_object(obj);
  82.         obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  83.     }
  84.  
  85.     if (obj->madv == I915_MADV_DONTNEED)
  86.         obj->dirty = 0;
  87.  
  88.     obj->dirty = 0;
  89. }
  90.  
  91. static const struct drm_i915_gem_object_ops bitmap_object_ops = {
  92.     .get_pages = bitmap_get_pages_gtt,
  93.     .put_pages = bitmap_put_pages_gtt,
  94. };
  95.  
  96.  
  97.  
  98. #if 0
  99. struct  io_call_10         /*     SRV_CREATE_SURFACE    */
  100. {
  101.     u32     handle;       // ignored
  102.     void   *data;         // ignored
  103.  
  104.     u32     width;
  105.     u32     height;
  106.     u32     pitch;        // ignored
  107.  
  108.     u32     max_width;
  109.     u32     max_height;
  110.     u32     format;       // reserved mbz
  111. };
  112.  
  113. #endif
  114.  
  115. int create_surface(struct drm_device *dev, struct io_call_10 *pbitmap)
  116. {
  117.     struct drm_i915_gem_object *obj;
  118.  
  119.     bitmap_t   *bitmap;
  120.     u32         handle;
  121.     u32         width, max_width;
  122.     u32         height, max_height;
  123.     u32         size,  max_size;
  124.     u32         pitch, max_pitch;
  125.     void       *uaddr;
  126.     dma_addr_t *pages;
  127.     u32         page_count;
  128.  
  129.     int         i;
  130.  
  131.     int   ret;
  132.  
  133.     pbitmap->handle = 0;
  134.     pbitmap->data   = (void*)-1;
  135.  
  136.     width  = pbitmap->width;
  137.     height = pbitmap->height;
  138.  
  139.     if((width == 0)||(height == 0)||(width > 4096)||(height > 4096))
  140.         goto err1;
  141.  
  142. /*
  143.     if( ((pbitmap->max_width !=0 ) &&
  144.          (pbitmap->max_width < width)) ||
  145.          (pbitmap->max_width > 4096) )
  146.         goto err1;
  147.  
  148.     if( ((pbitmap->max_height !=0 ) &&
  149.          (pbitmap->max_height < width)) ||
  150.          (pbitmap->max_height > 4096) )
  151.         goto err1;
  152.  
  153.     if( pbitmap->format != 0)
  154.         goto err1;
  155. */
  156.  
  157.     max_width  = (pbitmap->max_width ==0) ? width  : pbitmap->max_width;
  158.     max_height = (pbitmap->max_height==0) ? height : pbitmap->max_height;
  159.  
  160.     handle = alloc_handle(&bm_mm);
  161. //    printf("%s %d\n",__FUNCTION__, handle);
  162.  
  163.     if(handle == 0)
  164.         goto err1;
  165.  
  166.     bitmap = CreateObject(GetPid(), sizeof(*bitmap));
  167. //    printf("bitmap %x\n", bitmap);
  168.     if( bitmap == NULL)
  169.         goto err2;
  170.  
  171.     bitmap->handle = handle;
  172.     bitmap->header.destroy = destroy_bitmap;
  173.     bitmap->obj    = NULL;
  174.  
  175.     hmm_set_data(&bm_mm, handle, bitmap);
  176.  
  177.     pitch = ALIGN(width*4,64);
  178.     size =  roundup(pitch*height, PAGE_SIZE);
  179.  
  180. //    printf("pitch %d size %d\n", pitch, size);
  181.  
  182.     max_pitch = ALIGN(max_width*4,64);
  183.     max_size =  roundup(max_pitch*max_height, PAGE_SIZE);
  184.  
  185. //    printf("max_pitch %d max_size %d\n", max_pitch, max_size);
  186.  
  187.     uaddr = UserAlloc(max_size);
  188.     if( uaddr == NULL)
  189.         goto err3;
  190.     else
  191.     {
  192.         u32           max_count;
  193.         dma_addr_t    page;
  194.         char *vaddr = uaddr;
  195.  
  196.         page_count = size/PAGE_SIZE;
  197.         max_count = max_size/PAGE_SIZE;
  198.  
  199.         pages = kzalloc(max_count*sizeof(dma_addr_t), 0);
  200.         if( pages == NULL)
  201.             goto err4;
  202.  
  203.         for(i = 0; i < page_count; i++, vaddr+= PAGE_SIZE)
  204.         {
  205.             page = AllocPage();
  206.             if ( page == 0 )
  207.                 goto err4;
  208.             pages[i] = page;
  209.  
  210.             MapPage(vaddr, page, 0x207);        //map as shared page
  211.         };
  212.         bitmap->page_count = page_count;
  213.         bitmap->max_count  = max_count;
  214.     };
  215.  
  216.     DRM_DEBUG("%s alloc %d pages\n", __FUNCTION__, page_count);
  217.  
  218.     obj = i915_gem_alloc_object(dev, size);
  219.     if (obj == NULL)
  220.         goto err4;
  221.  
  222.     obj->ops = &bitmap_object_ops;
  223.     obj->allocated_pages = pages;
  224.  
  225.     ret = i915_gem_object_pin(obj, PAGE_SIZE, true,true);
  226.     if (ret)
  227.         goto err5;
  228.  
  229.     obj->mapped = uaddr ;
  230.  
  231.     bitmap->handle = handle;
  232.     bitmap->uaddr  = uaddr;
  233.     bitmap->pitch  = pitch;
  234.     bitmap->gaddr  = obj->gtt_offset;
  235.  
  236.     bitmap->width  = width;
  237.     bitmap->height = height;
  238.     bitmap->max_width  = max_width;
  239.     bitmap->max_height = max_height;
  240.  
  241.     bitmap->obj    = obj;
  242.     bitmap->header.destroy = destroy_bitmap;
  243.  
  244.     pbitmap->handle = handle;
  245.     pbitmap->data   = uaddr;
  246.     pbitmap->pitch  = pitch;
  247.  
  248.  
  249.     DRM_DEBUG("%s handle: %d pitch: %d gpu_addr: %x user_addr: %x\n",
  250.             __FUNCTION__, handle, pitch, obj->gtt_offset, uaddr);
  251.  
  252.     return 0;
  253.  
  254. err5:
  255.     mutex_lock(&dev->struct_mutex);
  256.     drm_gem_object_unreference(&obj->base);
  257.     mutex_unlock(&dev->struct_mutex);
  258.  
  259. err4:
  260.     while (i--)
  261.         FreePage(pages[i]);
  262.     free(pages);
  263.     UserFree(uaddr);
  264.  
  265. err3:
  266.     __DestroyObject(bitmap);
  267. err2:
  268.     free_handle(&bm_mm, handle);
  269. err1:
  270.     return -1;
  271. };
  272.  
  273.  
  274. int lock_surface(struct io_call_12 *pbitmap)
  275. {
  276.     int ret;
  277.  
  278.     bitmap_t  *bitmap;
  279.  
  280.     if(unlikely(pbitmap->handle == 0))
  281.         return -1;
  282.  
  283.     bitmap = (bitmap_t*)hmm_get_data(&bm_mm, pbitmap->handle);
  284.  
  285.     if(unlikely(bitmap==NULL))
  286.         return -1;
  287.  
  288.     mutex_lock(&main_device->struct_mutex);
  289.     ret = i915_gem_object_set_to_cpu_domain(bitmap->obj, true);
  290.     mutex_unlock(&main_device->struct_mutex);
  291.  
  292.     if(ret != 0 )
  293.     {
  294.         pbitmap->data  = NULL;
  295.         pbitmap->pitch = 0;
  296.  
  297.         dbgprintf("%s fail\n", __FUNCTION__);
  298.         return ret;
  299.     };
  300.  
  301.     pbitmap->data  = bitmap->uaddr;
  302.     pbitmap->pitch = bitmap->pitch;
  303.  
  304.     return 0;
  305. };
  306.  
  307.  
  308.  
  309.  
  310. int init_bitmaps()
  311. {
  312.     int ret;
  313.  
  314.     ret = init_hmm(&bm_mm, 1024);
  315.  
  316.     return ret;
  317. };
  318.  
  319.  
  320.  
  321. int get_driver_caps(hwcaps_t *caps)
  322. {
  323.     int ret = 0;
  324.  
  325.     switch(caps->idx)
  326.     {
  327.         case 0:
  328.             caps->opt[0] = DRIVER_CAPS_0;
  329.             caps->opt[1] = DRIVER_CAPS_1;
  330.             break;
  331.  
  332.         case 1:
  333.             caps->cap1.max_tex_width  = 4096;
  334.             caps->cap1.max_tex_height = 4096;
  335.             break;
  336.         default:
  337.             ret = 1;
  338.     };
  339.     caps->idx = 1;
  340.     return ret;
  341. }
  342.  
  343.  
  344. void __attribute__((regparm(1))) destroy_context(struct context *context)
  345. {
  346.     DRM_DEBUG("destroy context %x\n", context);
  347.  
  348.     context_map[context->slot] = NULL;
  349.  
  350.     mutex_lock(&main_device->struct_mutex);
  351.     drm_gem_object_unreference(&context->obj->base);
  352.     mutex_unlock(&main_device->struct_mutex);
  353.  
  354.     __DestroyObject(context);
  355. };
  356.  
  357.  
  358. #define CURRENT_TASK             (0x80003000)
  359.  
  360. struct context *get_context(struct drm_device *dev)
  361. {
  362.     struct context *context;
  363.     struct io_call_10 io_10;
  364.     int    slot = *((u8*)CURRENT_TASK);
  365.     int    ret;
  366.  
  367.     context = context_map[slot];
  368.  
  369.     if( context != NULL)
  370.         return context;
  371.  
  372.     context = CreateObject(GetPid(), sizeof(*context));
  373.  
  374.     if( context != NULL)
  375.     {
  376.         drm_i915_private_t *dev_priv = dev->dev_private;
  377.         struct drm_i915_gem_object *obj;
  378.  
  379.         obj = i915_gem_alloc_object(dev, 4096);
  380.         i915_gem_object_pin(obj, 4096, true, true);
  381.  
  382.         context->obj = obj;
  383.         context->cmd_buffer = MapIoMem((addr_t)obj->pages.page[0], 4096, PG_SW|PG_NOCACHE);
  384.         context->cmd_offset = obj->gtt_offset;
  385.  
  386.         context->header.destroy = destroy_context;
  387.         context->mask  = NULL;
  388.         context->seqno = 0;
  389.         context->slot  = slot;
  390.  
  391.         context_map[slot] = context;
  392.     };
  393.     return context;
  394. };
  395.  
  396.