Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Copyright 2008 Advanced Micro Devices, Inc.
  3.  * Copyright 2008 Red Hat Inc.
  4.  * Copyright 2009 Jerome Glisse.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in
  14.  * all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22.  * OTHER DEALINGS IN THE SOFTWARE.
  23.  *
  24.  * Authors: Dave Airlie
  25.  *          Alex Deucher
  26.  *          Jerome Glisse
  27.  */
  28. #include <linux/seq_file.h>
  29. #include <linux/slab.h>
  30. #include "drmP.h"
  31. #include "radeon_drm.h"
  32. #include "radeon_reg.h"
  33. #include "radeon.h"
  34. #include "atom.h"
  35.  
  36. int radeon_debugfs_ib_init(struct radeon_device *rdev);
  37.  
  38. /*
  39.  * IB.
  40.  */
  41. int radeon_ib_get(struct radeon_device *rdev, struct radeon_ib **ib)
  42. {
  43.         struct radeon_fence *fence;
  44.         struct radeon_ib *nib;
  45.         int r = 0, i, c;
  46.  
  47.         *ib = NULL;
  48.         r = radeon_fence_create(rdev, &fence);
  49.         if (r) {
  50.                 dev_err(rdev->dev, "failed to create fence for new IB\n");
  51.                 return r;
  52.         }
  53.     mutex_lock(&rdev->ib_pool.mutex);
  54.         for (i = rdev->ib_pool.head_id, c = 0, nib = NULL; c < RADEON_IB_POOL_SIZE; c++, i++) {
  55.                 i &= (RADEON_IB_POOL_SIZE - 1);
  56.                 if (rdev->ib_pool.ibs[i].free) {
  57.                         nib = &rdev->ib_pool.ibs[i];
  58.                         break;
  59.         }
  60.         }
  61.         if (nib == NULL) {
  62.                 /* This should never happen, it means we allocated all
  63.                  * IB and haven't scheduled one yet, return EBUSY to
  64.                  * userspace hoping that on ioctl recall we get better
  65.                  * luck
  66.                  */
  67.                 dev_err(rdev->dev, "no free indirect buffer !\n");
  68.                 mutex_unlock(&rdev->ib_pool.mutex);
  69.                 radeon_fence_unref(&fence);
  70.                 return -EBUSY;
  71.         }
  72.         rdev->ib_pool.head_id = (nib->idx + 1) & (RADEON_IB_POOL_SIZE - 1);
  73.         nib->free = false;
  74. //   if (nib->fence) {
  75. //       mutex_unlock(&rdev->ib_pool.mutex);
  76. //   r = radeon_fence_wait(nib->fence, false);
  77. //   if (r) {
  78. //           dev_err(rdev->dev, "error waiting fence of IB(%u:0x%016lX:%u)\n",
  79. //               nib->idx, (unsigned long)nib->gpu_addr, nib->length_dw);
  80. //           mutex_lock(&rdev->ib_pool.mutex);
  81. //           nib->free = true;
  82. //           mutex_unlock(&rdev->ib_pool.mutex);
  83. //           radeon_fence_unref(&fence);
  84. //           return r;
  85. //       }
  86. //       mutex_lock(&rdev->ib_pool.mutex);
  87. //   }
  88.         radeon_fence_unref(&nib->fence);
  89.         nib->fence = fence;
  90.         nib->length_dw = 0;
  91.         mutex_unlock(&rdev->ib_pool.mutex);
  92.         *ib = nib;
  93.         return 0;
  94. }
  95.  
  96. void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
  97. {
  98.         struct radeon_ib *tmp = *ib;
  99.  
  100.         *ib = NULL;
  101.         if (tmp == NULL) {
  102.                 return;
  103.         }
  104.         if (!tmp->fence->emited)
  105.                 radeon_fence_unref(&tmp->fence);
  106.         mutex_lock(&rdev->ib_pool.mutex);
  107.         tmp->free = true;
  108.         mutex_unlock(&rdev->ib_pool.mutex);
  109. }
  110.  
  111. int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
  112. {
  113.         int r = 0;
  114.  
  115.         if (!ib->length_dw || !rdev->cp.ready) {
  116.                 /* TODO: Nothings in the ib we should report. */
  117.                 DRM_ERROR("radeon: couldn't schedule IB(%u).\n", ib->idx);
  118.                 return -EINVAL;
  119.         }
  120.  
  121.         /* 64 dwords should be enough for fence too */
  122.         r = radeon_ring_lock(rdev, 64);
  123.         if (r) {
  124.                 DRM_ERROR("radeon: scheduling IB failed (%d).\n", r);
  125.                 return r;
  126.         }
  127.         radeon_ring_ib_execute(rdev, ib);
  128.         radeon_fence_emit(rdev, ib->fence);
  129.         mutex_lock(&rdev->ib_pool.mutex);
  130.         /* once scheduled IB is considered free and protected by the fence */
  131.         ib->free = true;
  132.         mutex_unlock(&rdev->ib_pool.mutex);
  133.         radeon_ring_unlock_commit(rdev);
  134.         return 0;
  135. }
  136.  
  137. int radeon_ib_pool_init(struct radeon_device *rdev)
  138. {
  139.         void *ptr;
  140.         uint64_t gpu_addr;
  141.         int i;
  142.         int r = 0;
  143.  
  144.         if (rdev->ib_pool.robj)
  145.                 return 0;
  146.         INIT_LIST_HEAD(&rdev->ib_pool.bogus_ib);
  147.         /* Allocate 1M object buffer */
  148.         r = radeon_bo_create(rdev, RADEON_IB_POOL_SIZE*64*1024,
  149.                              PAGE_SIZE, true, RADEON_GEM_DOMAIN_GTT,
  150.                                 &rdev->ib_pool.robj);
  151.         if (r) {
  152.                 DRM_ERROR("radeon: failed to ib pool (%d).\n", r);
  153.                 return r;
  154.         }
  155.         r = radeon_bo_reserve(rdev->ib_pool.robj, false);
  156.         if (unlikely(r != 0))
  157.                 return r;
  158.         r = radeon_bo_pin(rdev->ib_pool.robj, RADEON_GEM_DOMAIN_GTT, &gpu_addr);
  159.         if (r) {
  160.                 radeon_bo_unreserve(rdev->ib_pool.robj);
  161.                 DRM_ERROR("radeon: failed to pin ib pool (%d).\n", r);
  162.                 return r;
  163.         }
  164.         r = radeon_bo_kmap(rdev->ib_pool.robj, &ptr);
  165.         radeon_bo_unreserve(rdev->ib_pool.robj);
  166.         if (r) {
  167.                 DRM_ERROR("radeon: failed to map ib pool (%d).\n", r);
  168.                 return r;
  169.         }
  170.         for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  171.                 unsigned offset;
  172.  
  173.                 offset = i * 64 * 1024;
  174.                 rdev->ib_pool.ibs[i].gpu_addr = gpu_addr + offset;
  175.                 rdev->ib_pool.ibs[i].ptr = ptr + offset;
  176.                 rdev->ib_pool.ibs[i].idx = i;
  177.                 rdev->ib_pool.ibs[i].length_dw = 0;
  178.                 rdev->ib_pool.ibs[i].free = true;
  179.         }
  180.         rdev->ib_pool.head_id = 0;
  181.         rdev->ib_pool.ready = true;
  182.         DRM_INFO("radeon: ib pool ready.\n");
  183.         if (radeon_debugfs_ib_init(rdev)) {
  184.                 DRM_ERROR("Failed to register debugfs file for IB !\n");
  185.         }
  186.         return r;
  187. }
  188.  
  189. void radeon_ib_pool_fini(struct radeon_device *rdev)
  190. {
  191.         int r;
  192.         struct radeon_bo *robj;
  193.  
  194.         if (!rdev->ib_pool.ready) {
  195.                 return;
  196.         }
  197.         mutex_lock(&rdev->ib_pool.mutex);
  198. //   radeon_ib_bogus_cleanup(rdev);
  199.         robj = rdev->ib_pool.robj;
  200.         rdev->ib_pool.robj = NULL;
  201.         mutex_unlock(&rdev->ib_pool.mutex);
  202.  
  203.         if (robj) {
  204.                 r = radeon_bo_reserve(robj, false);
  205.                 if (likely(r == 0)) {
  206.                         radeon_bo_kunmap(robj);
  207.                         radeon_bo_unpin(robj);
  208.                         radeon_bo_unreserve(robj);
  209.                 }
  210.                 radeon_bo_unref(&robj);
  211.         }
  212. }
  213.  
  214.  
  215. /*
  216.  * Ring.
  217.  */
  218. void radeon_ring_free_size(struct radeon_device *rdev)
  219. {
  220.         if (rdev->wb.enabled)
  221.                 rdev->cp.rptr = le32_to_cpu(rdev->wb.wb[RADEON_WB_CP_RPTR_OFFSET/4]);
  222.         else {
  223.         if (rdev->family >= CHIP_R600)
  224.                 rdev->cp.rptr = RREG32(R600_CP_RB_RPTR);
  225.         else
  226.         rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
  227.         }
  228.         /* This works because ring_size is a power of 2 */
  229.         rdev->cp.ring_free_dw = (rdev->cp.rptr + (rdev->cp.ring_size / 4));
  230.         rdev->cp.ring_free_dw -= rdev->cp.wptr;
  231.         rdev->cp.ring_free_dw &= rdev->cp.ptr_mask;
  232.         if (!rdev->cp.ring_free_dw) {
  233.                 rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  234.         }
  235. }
  236.  
  237. int radeon_ring_alloc(struct radeon_device *rdev, unsigned ndw)
  238. {
  239.         int r;
  240.  
  241.         /* Align requested size with padding so unlock_commit can
  242.          * pad safely */
  243.         ndw = (ndw + rdev->cp.align_mask) & ~rdev->cp.align_mask;
  244.         while (ndw > (rdev->cp.ring_free_dw - 1)) {
  245.                 radeon_ring_free_size(rdev);
  246.                 if (ndw < rdev->cp.ring_free_dw) {
  247.                         break;
  248.                 }
  249. //        r = radeon_fence_wait_next(rdev);
  250. //       if (r) {
  251. //           mutex_unlock(&rdev->cp.mutex);
  252. //           return r;
  253. //       }
  254.         }
  255.         rdev->cp.count_dw = ndw;
  256.         rdev->cp.wptr_old = rdev->cp.wptr;
  257.         return 0;
  258. }
  259.  
  260. int radeon_ring_lock(struct radeon_device *rdev, unsigned ndw)
  261. {
  262.         int r;
  263.  
  264.         mutex_lock(&rdev->cp.mutex);
  265.         r = radeon_ring_alloc(rdev, ndw);
  266.         if (r) {
  267.                 mutex_unlock(&rdev->cp.mutex);
  268.                 return r;
  269.         }
  270.         return 0;
  271. }
  272.  
  273. void radeon_ring_commit(struct radeon_device *rdev)
  274. {
  275.         unsigned count_dw_pad;
  276.         unsigned i;
  277.  
  278.         /* We pad to match fetch size */
  279.         count_dw_pad = (rdev->cp.align_mask + 1) -
  280.                        (rdev->cp.wptr & rdev->cp.align_mask);
  281.         for (i = 0; i < count_dw_pad; i++) {
  282.                 radeon_ring_write(rdev, 2 << 30);
  283.         }
  284.         DRM_MEMORYBARRIER();
  285.         radeon_cp_commit(rdev);
  286. }
  287.  
  288. void radeon_ring_unlock_commit(struct radeon_device *rdev)
  289. {
  290.         radeon_ring_commit(rdev);
  291.         mutex_unlock(&rdev->cp.mutex);
  292. }
  293.  
  294. void radeon_ring_unlock_undo(struct radeon_device *rdev)
  295. {
  296.         rdev->cp.wptr = rdev->cp.wptr_old;
  297.         mutex_unlock(&rdev->cp.mutex);
  298. }
  299.  
  300. int radeon_ring_init(struct radeon_device *rdev, unsigned ring_size)
  301. {
  302.         int r;
  303.  
  304.         rdev->cp.ring_size = ring_size;
  305.     /* Allocate ring buffer */
  306.         if (rdev->cp.ring_obj == NULL) {
  307.                 r = radeon_bo_create(rdev, rdev->cp.ring_size, PAGE_SIZE, true,
  308.                                          RADEON_GEM_DOMAIN_GTT,
  309.                                          &rdev->cp.ring_obj);
  310.                 if (r) {
  311.                         dev_err(rdev->dev, "(%d) ring create failed\n", r);
  312.                         return r;
  313.                 }
  314.                 r = radeon_bo_reserve(rdev->cp.ring_obj, false);
  315.                 if (unlikely(r != 0))
  316.                         return r;
  317.                 r = radeon_bo_pin(rdev->cp.ring_obj, RADEON_GEM_DOMAIN_GTT,
  318.                                       &rdev->cp.gpu_addr);
  319.                 if (r) {
  320.                         radeon_bo_unreserve(rdev->cp.ring_obj);
  321.                         dev_err(rdev->dev, "(%d) ring pin failed\n", r);
  322.                         return r;
  323.                 }
  324.                 r = radeon_bo_kmap(rdev->cp.ring_obj,
  325.                                        (void **)&rdev->cp.ring);
  326.                 radeon_bo_unreserve(rdev->cp.ring_obj);
  327.                 if (r) {
  328.                         dev_err(rdev->dev, "(%d) ring map failed\n", r);
  329.                         return r;
  330.                 }
  331.         }
  332.         rdev->cp.ptr_mask = (rdev->cp.ring_size / 4) - 1;
  333.         rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  334.         return 0;
  335. }
  336.  
  337. void radeon_ring_fini(struct radeon_device *rdev)
  338. {
  339.         int r;
  340.         struct radeon_bo *ring_obj;
  341.  
  342.         mutex_lock(&rdev->cp.mutex);
  343.         ring_obj = rdev->cp.ring_obj;
  344.         rdev->cp.ring = NULL;
  345.         rdev->cp.ring_obj = NULL;
  346.         mutex_unlock(&rdev->cp.mutex);
  347.  
  348.         if (ring_obj) {
  349.                 r = radeon_bo_reserve(ring_obj, false);
  350.                 if (likely(r == 0)) {
  351.                         radeon_bo_kunmap(ring_obj);
  352.                         radeon_bo_unpin(ring_obj);
  353.                         radeon_bo_unreserve(ring_obj);
  354.                 }
  355.                 radeon_bo_unref(&ring_obj);
  356.         }
  357. }
  358.  
  359.  
  360. /*
  361.  * Debugfs info
  362.  */
  363. #if defined(CONFIG_DEBUG_FS)
  364. static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
  365. {
  366.         struct drm_info_node *node = (struct drm_info_node *) m->private;
  367.         struct radeon_ib *ib = node->info_ent->data;
  368.         unsigned i;
  369.  
  370.         if (ib == NULL) {
  371.                 return 0;
  372.         }
  373.         seq_printf(m, "IB %04u\n", ib->idx);
  374.         seq_printf(m, "IB fence %p\n", ib->fence);
  375.         seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  376.         for (i = 0; i < ib->length_dw; i++) {
  377.                 seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  378.         }
  379.         return 0;
  380. }
  381.  
  382. static int radeon_debugfs_ib_bogus_info(struct seq_file *m, void *data)
  383. {
  384.         struct drm_info_node *node = (struct drm_info_node *) m->private;
  385.         struct radeon_device *rdev = node->info_ent->data;
  386.         struct radeon_ib *ib;
  387.         unsigned i;
  388.  
  389.         mutex_lock(&rdev->ib_pool.mutex);
  390.         if (list_empty(&rdev->ib_pool.bogus_ib)) {
  391.                 mutex_unlock(&rdev->ib_pool.mutex);
  392.                 seq_printf(m, "no bogus IB recorded\n");
  393.                 return 0;
  394.         }
  395.         ib = list_first_entry(&rdev->ib_pool.bogus_ib, struct radeon_ib, list);
  396.         list_del_init(&ib->list);
  397.         mutex_unlock(&rdev->ib_pool.mutex);
  398.         seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  399.         for (i = 0; i < ib->length_dw; i++) {
  400.                 seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  401.         }
  402.         vfree(ib->ptr);
  403.         kfree(ib);
  404.         return 0;
  405. }
  406.  
  407. static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
  408. static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
  409.  
  410. static struct drm_info_list radeon_debugfs_ib_bogus_info_list[] = {
  411.         {"radeon_ib_bogus", radeon_debugfs_ib_bogus_info, 0, NULL},
  412. };
  413. #endif
  414.  
  415. int radeon_debugfs_ib_init(struct radeon_device *rdev)
  416. {
  417. #if defined(CONFIG_DEBUG_FS)
  418.         unsigned i;
  419.         int r;
  420.  
  421.         radeon_debugfs_ib_bogus_info_list[0].data = rdev;
  422.         r = radeon_debugfs_add_files(rdev, radeon_debugfs_ib_bogus_info_list, 1);
  423.         if (r)
  424.                 return r;
  425.         for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  426.                 sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
  427.                 radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
  428.                 radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
  429.                 radeon_debugfs_ib_list[i].driver_features = 0;
  430.                 radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
  431.         }
  432.         return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
  433.                                         RADEON_IB_POOL_SIZE);
  434. #else
  435.         return 0;
  436. #endif
  437. }
  438.